js中的call()函数,简单的说就是用来纠正指正的吧!
调用一个对象的方法,用另一个对象替换当前对象,这样说显得相当的空洞与抽象,实例说明一切!
1 <script type="text/javascript"> 2 function person(name){//带参数的构造函数 3 this.name=name; //定义并初始化属性 4 this.SayHello=function(){//定义对象方法 5 alert("hello I`m " + this.name); 6 } 7 } 8 9 function Employee(name,salary){ 10 11 person.call(this,name);//用call调用父构造函数,this指当前的构造函数Employee,所以就是用person替换Employee 12 this.salary=salary;
13 this.showMe=function(){ 14 alert(this.name + "$" + this.salary); 15 } 16 } 17 18 var BillGates=new person("Bill Gates");//创建person类的Billgates对象 19 var SteveJobs=new Employee("Steve Jobs",1234);//创建Employee类的stevejobs对象 20 21 BillGates.SayHello();//输出hello I`m Bill Gates 22 SteveJobs.SayHello();//输出hello I`m Steve Jobs 23 SteveJobs.showMe();//输出Steve Jobs $ 1234 24 </script>
本人菜鸟一只!解释不清楚,请见谅!