首页 > web前端 > js教程 > 正文

调用模式与this绑定

php中世界最好的语言
发布: 2018-06-06 17:41:49
原创
1696人浏览过

这次给大家带来调用模式与this绑定,调用模式与this绑定的注意事项有哪些,下面就是实战案例,一起来看一下。

Invocation 调用

调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数。

实参与形参不一致不会导致运行时错误,多的被忽略,少的补为undefined

每个方法都会收到两个附加参数:this和arguments。this的值取决于调用的模式,调用模式:方法,函数,构造器和apply调用模式
this被赋值发生在被调用的时刻。不同的调用模式可以用call方法实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

var myObject = {

value: 0,

increment: function (inc) {

this.value += typeof inc === 'number' ? inc : 1;

}

};

myObject.double = function(){

var helper = function(){

console.log(this);// this point to window

}

console.log(this);// this point to object myObject

helper();

}

myObject.double();//myObject Window

登录后复制

1 The Method Invocation Pattern 方法调用模式

方法:函数被保存为对象的属性.当方法被调用时,this被绑定到该对象

公共方法:通过this取得他们所属对象的上下文的方法

1

2

myObject.increment();

document.writeln(myObject.value); //

登录后复制

底层实现: myObject.increment。call(myObject,0);

2 The Function Invocation Pattern 函数调用模式

当函数并非对象的属性时就被当作函数调用(有点废话。。),this被绑定到全局对象(window)

ECMAScript5中新增strict mode, 在这种模式中,为了尽早的暴露出问题,方便调试。this被绑定为undefined

1

2

var add = function (a,b) { return a + b;};

var sum = add(3,4);// sum的值为7

登录后复制

底层实现:add.call(window,3,4)

1

strict mode:add.call(undefined,3,4)

登录后复制

方法调用模式和函数调用模式的区别

1

2

3

4

5

6

7

function hello(thing) {

console.log(this + " says hello " + thing);

}

person = { name: "Brendan Eich" }

person.hello = hello;

person.hello("world") // [object Object] says hello world 等价于 person。hello。call(person,“world”)

hello("world") // "[object DOMWindow]world" 等价于 hello。call(window,“world”)

登录后复制

3 The Constructor Invocation Pattern

JavaScript是基于原型继承的语言,同时提供了一套基于类的语言的对象构建语法。

this指向new返回的对象

1

2

3

4

5

6

7

8

var Quo = function (string) {

this.status = string;

};

Quo.prototype.get_status = function ( ) {

return this.status;

};

var myQuo = new Quo("this is new quo"); //new容易漏写,有更优替换

myQuo.get_status( );// this is new quo

登录后复制

4 The Apply Invocation Pattern

apply和call是javascript的内置参数,都是立刻将this绑定到函数,前者参数是数组,后者要一个个的传递apply也是由call底层实现的

1

2

3

4

5

6

7

8

9

10

11

apply(this,arguments[]);

call(this,arg1,arg2...);

var person = {

name: "James Smith",

hello: function(thing,thing2) {

console.log(this.name + " says hello " + thing + thing2);

}

}

person.hello.call({ name: "Jim Smith" },"world","!"); // output: "Jim Smith says hello world!"

var args = ["world","!"];

person.hello.apply({ name: "Jim Smith" },args); // output: "Jim Smith says hello world!"

登录后复制

相对的,bind函数将绑定this到函数和调用函数分离开来,使得函数可以在一个特定的上下文中调用,尤其是事件bind的apply实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Function.prototype.bind = function(ctx){

var fn = this; //fn是绑定的function

return function(){

fn.apply(ctx, arguments);

};

};

bind用于事件中

function MyObject(element) {

this.elm = element;

element.addEventListener('click', this.onClick.bind(this), false);

};

//this对象指向的是MyObject的实例

MyObject.prototype.onClick = function(e) {

var t=this; //do something with [t]...

};

登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

nodejs操作excel文件

怎样做出下滑页面底部无限加载数据需求

以上就是调用模式与this绑定的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号