JS函数调用

作者: 小默 分类: php开发 发布时间: 2015-07-30 05:33 ė
Warning: Use of undefined constant the_views - assumed 'the_views' (this will throw an Error in a future version of PHP) in /www/wwwroot/www.seohave.com/wp-content/themes/TangStyle/single.php on line 19
11,893 人访问
6没有评论

js的函数调用有两个额外的参数 this 和 arguments 。arguments是参数组,他并不是一个真实的数组,但是可以使用.length方法获得长度。

书上有说4种调用方式:

  1. 方法调用模式
  2. 函数调用模式
  3. 构造器调用模式
  4. apply调用模式

下面我们来看一些实例会更好理解:

1、方法调用模式。

请注意this此时指向myobject。

/*方法调用模式*/
var myobject={
value:0,
inc:function(){
alert(this.value)
}
}
myobject.inc()

 

2、函数调用模式

请注意this此时指向window

/*函数调用模式*/

var add=function(a,b){
alert(this)//this被绑顶到window
return a+b;
}
var sum=add(3,4);
alert(sum)

 

3、构造器调用模式

javascript语言精粹本书书建议摒弃这种方式,因为有更好的方式。这里先不介绍。

会在这里加一个连接。

/*构造器调用模式  摒弃*/

var quo=function(string){
this.status=string;
}
quo.prototype.get_status=function(){
return this.status;
}
var qq=new quo(“aaa”);
alert(qq.get_status());

 

4、apply调用模式

我们可以来看一个更有用的apply实例,看最下面的代码。

 

/*apply*/
//注意使用了上面的sum函数
//这种调用方式的优点在于可以指向this指向的对象。
//apply的第一个参数就是this指针要指向的对象
var arr=[10,20];
var sum=add.apply(myobject,arr);
alert(sum);

 

看apply真正应用。bind这是一个绑定时间的函数

var bind=function(object,type,fn){
if(object.attachEvent){//IE浏览器
object.attachEvent(“on”+type,(function(){
return function(event){
window.event.cancelBubble=true;//停止时间冒泡
object.attachEvent=[fn.apply(object)];//—-这里我要讲的是这里
//在IE里用attachEvent添加一个时间绑定以后。
//this的指向不是到object对象本身所以。我们绑定的function里的this.id是无法正常工作的。
//但是如果我们用fn.apply(object)
//这里可以看出我们是把apply的第一个对象也就是this的指向变更给了object所以this.id就变成了
//object.id 可以正常工作了。

}
})(object),false);
}else if(object.addEventListener){//其他浏览器
object.addEventListener(type,function(event){
event.stopPropagation();//停止时间冒泡
fn.apply(this)
});
}

}
bind(document.getElementById(“aaa”),”click”,function(){alert(this.id)});

以上我为初学者专门写的JS函数调用方法,希望大家好好看看,我所说的都是苏州php的一些经验,不过是全国通用的。

.021355

本文出自 建站seo运营,转载时请注明出处及相应链接。

本文永久链接: https://www.seohave.com/238.html

发表回复

Ɣ回顶部