日期:2014-05-16 浏览次数:20479 次
function myprint(s1,s2,s3) { alert(s1+"_"+s2+"_"+s3); } myprint(); //undefined_undefined_undefined myprint("string1","string2"); //string1_string2_undefined myprint("string1","string2","string3","string4"); //string1_string2_string3
function myprint(s1,person) { var defaultperson = { //默认person对象 "name":"name1", "age":18, "sex":"female" }; if(!s1) { //s1不允许为空 alert("s1 must be input!"); return false; } person = person || defaultperson; //接受person对象参数 alert(s1+"_"+person.name+":"+person.age+":"+person.sex); }; myprint(); //s1 must be input! myprint("s1"); //s1_name1:18:female myprint("s1",{"name":"sdcyst","age":23,"sex":"male"}); //s1_sdcyst:23:male
function myargs() { alert(arguments.length); alert(arguments[0]); } myargs(); //0 --- undefined myargs("1",[1,2]); //2 --- 1
function(x) { if (x <= 1) return 1; return x * arguments.callee(x-1); } (section8.2)
var obj = {f0:function(){alert("f0");}}; //对象包含一个方法 function f1() {alert("f1");} obj.f1 = f1; //为对象添加方法 obj.f0(); //f0 f0是obj的方法 obj.f1(); //f1 f1是obj的方法 f1(); //f1 f1同时又是一个函数,可以直接调用 f0(); //f0仅仅是obj的方法,只能通过对象来调用
var obj = {"name":"NAME","sex":"female"}; obj.print = function() { //为对象添加方法 alert(this.name + "_" + this["sex"]); }; obj.print(); //NAME_female obj.sex = "male"; obj.print(); //NAME_male var obj = {"name":"NAME","sex":"female"}; obj.print = function() { //为对象添加方法 alert(this.name + "_" + this["sex"]); }; obj.print(); //NAME_female obj.sex = "male"; obj.print(); //NAME_male
var person = {name:"defaultname", setName:function(s){ this.name = s; }, "printName":function(){ alert(this.name); }} person.printName(); //defaultname person.setName("newName"); person.printName(); //newName