阅读前需要理解的:
JS中 没有new 的对象在对象里的this是不存在的
例如:
function a(){
this.b="hi";
this.c=function(){alert('hi');}
}
alert(a.b);//out undefined
try{
a.c();
}catch(e){
alert(e->getmessage());//有输出
}
var a =new a();
alert(a.b);//输出 hi
a.c();//输出 hi
1、call
call 方法
调用一个对象的一个方法,以另一个对象替换当前对象。
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
参数
thisObj
可选项。将被用作当前对象的对象。
arg1, arg2, , argN
可选项。将被传递方法参数序列。
说明
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。
简单的列子(函数调用):
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,3,1);
这个例子中的意思就是用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4);
复杂点的例子(方法调用):
function Class1()
{
this.name = "class1";
this.showNam = function()
{
alert(this.name);
}
}
function Class2()
{
this.name = "class2";
}
var c1 = new Class1();
var c2 = new Class2();
c1.showNam.call(c2);
注意,call 的意思是把 c1 的方法放到c2上执行,原来c2是没有showNam() 方法,现在是把c1 的showNam()方法放到 c2 上来执行,所以this.name 应该是 class2,执行的结果就是:alert("class2");
实现继承
function Class1()
{
this.showTxt = function(txt)
{
alert(txt);
}
}
function Class2()
{
Class1.call(this);
}
var c2 = new Class2();
c2.showTxt("cc");
这样 Class2 就继承Class1了,Class1.call(this) 的 意思就是使用 Class1 对象代替this对象,那么 Class2 中不就有Class1 的所有属性和方法了吗,c2 对象就能够直接调用Class1 的方法以及属性了,执行结果就是:alert("cc");
对的,就是这样,这就是 javaScript 如何来模拟面向对象中的继承的,还可以实现多重继承。
多重继承
function Class10()
{
this.showSub = function(a,b)
{
alert(a-b);
}
}
function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}
function Class2()
{
Class10.call(this);
Class11.call(this);
}
很简单,使用两个 call 就实现多重继承了
当然,js的继承还有其他方法,例如使用原型链,这个不属于本文的范畴,只是在此说明call 的用法
重载:
function a(){
this.f=function(){
alert("af");
};
this.c=function(){
alert("ac");
}
}
function b(){
a.call(this);
var bf=this.f;
this.f=function(){
bf();//调用父类方法
alert("bf")
};
}
var ob=new b();
ob.f();//输出af,bf
ob.c();//输出ac
说了call ,当然还有 apply,这两个方法基本上是一个意思
区别在于 call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组。当参数明确时可用call, 当参数不明确时可用apply给合arguments
2、apply
对于apply和call两者在作用上是相同的,但两者在参数上有区别的。
对于第一个参数意义都一样,但对第二个参数:
apply传入的是一个参数数组,也就是将多个参数组合成为一个数组传入,而call则作为call的参数传入(从第二个参数开始)。
如 func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])
技巧(代码优雅而其执行效率高)
复制代码 代码如下:
alert(Math.max(5,8)) //8
alert(Math.max(5,7,9,3,1,6)) //9
var arr=[5,7,9,1]
alert(Math.max.apply(null,arr));
注意:call apply 是对类而言,也就是方法体而言,或对对象里的方法体而言,单纯对象本身没有这两个函数,如下例:
function a(){
this.f=function(){alert("af");}
}
function b(){
}
var oa=new a();
var ob=new b();
oa.call(ob);//错误的~~
a.call(ob);ob.f();//正确
oa.f.call(ob);//正确
3.arguments
arguments是一个类似数组但不是数组的对象,说它类似(仅仅是类似)数组是因为其具有数组一样的访问性质及方式,可以由 arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length。还有就是arguments对象存储的是实际传递给函数的参数,而不局限于函数声明所定义的参数列表(用funcName.length取),而且不能显式创建 arguments 对象。arguments 对象只有函数开始时才可用。
function ArgTest(a, b){
var i, s = "The ArgTest function expected ";
var numargs = arguments.length; // 获取被传递参数的数值。
var expargs = ArgTest.length; // 获取期望参数的数值。
if (expargs < 2)
s += expargs + " argument. ";
else
s += expargs + " arguments. ";
if (numargs < 2)
s += numargs + " was passed.";
else
s += numargs + " were passed.";
s += "\n\n"
for (i =0 ; i < numargs; i++){ // 获取参数内容。
s += " Arg " + i + " = " + arguments[i] + "\n";
}
return(s); // 返回参数列表。
}
window.οnlοad=function()
{
alert(ArgTest(1,2,3));
}
输出:The ArgTest function expected 2 arguments. 3 were passed.
Arg 0 =1
Arg 1 =2
Arg 2 =3
详细出处参考:http://www.jb51.net/article/25080.htm
:http://www.cnblogs.com/liushannet/archive/2010/10/28/1863999.html