functionPerson(age){this.age = age;var name ="123";this.sayName=function(){return name;}}var person =newPerson(12);
console.log(person.name+","+person.age);
console.log(person.sayName()+","+person.age);
调用函数的三种方式
1、直接调用函数
functionfun(){
console.log("aaa");}fun();
2、用call方法调用函数
vareach=function(array,fn){for(var index in array){
fn.call(null,index,array[index]);}}each([4,20,3],function(index,ele){
console.log(index+" "+ ele);})
vareach=function(array,fn){for(var index in array){
fn.apply(null,[index,array[index]]);}}each([4,20,3],function(index,ele){
console.log(index+" "+ ele);})