读书人

javascript apply步骤

发布时间: 2012-08-25 10:06:20 作者: rapoo

javascript apply方法
JavaScript的每个function对象都有一个apply方法
语法如下:funtion.apply([thisObj[,argArray]])
功能说明:将一个函数的对象上下文从初始的上下文改变为由thisObj指定的新对象,用来模拟多态

/**method 声明**/function Object1(name) {        this.name = name;        this.fun1 = function(){        alert("this is first method !");        };        this.fun2 = function() {        alert("this is second method !");        };}function Object2(name) {        Object1.apply(this,[name]);        //Object1.apply(this,["hahahhah"]);}/**method 调用**/        var o2 = new Object2("china");        o2.fun1();     //alert("this is first method!");        o2.fun2();   //alert("this is second method!");        alert(o2.name+"==================apply method!");//alert(china=============apply method!); 





读书人网 >JavaScript

热点推荐