javascript apply用法

来源:互联网 发布:wp config.php 修改 编辑:程序博客网 时间:2024/06/15 17:35

javascript apply用法

js中的apply方法类似于java中继承,例子如下:

function Parent(name,age){   //定义一个类,接下来的children累继承它
    this.name=name;     //名字  
    this.age=age;       //年龄
    this.sayhello=function(){alert(this.age)};
}

function Children(name,age,school){

   Parent.apply(this,arguments);//Children继承Person中的属性和方法,

   //并把自己的参数(name,age,school)传给Parent

}

var p1=new Parent("parent",10);

p1.sayhello(); //10

var s1=new Children("children",22,"清华大学");

s1.sayhello();//22  children 继承了 parent 并把参数传过去了


注:此处说的继承均是为了便于理解,js中并没有继承

0 0
原创粉丝点击