- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
第1节 JavaScript this关键字精解
1 JavaScript call()函数:
call()方法/apply()的意义在于扩充函数的作用域!
JavaScript func.call()定义:
若IsCallable(func)为false,则抛出TypeError异常。
将argList置空。
若方法调用不止一个参数,则按从左向右的顺序将参数添加到argList中。
返回func中的方法调用结果,提供thisArg作为this的值,并将argList作为参数列表。
Make an argument list (argList) out of parameters 1 through the end
The first parameter is thisValue
Invoke the function with this set to thisValue and the argList as its argument list
例子:
function hello(thing) {
console.log(this + says hello + thing);
}
hello.call(Yehuda, world) //= Yehuda says hello world
以上调用简化:
function hello(thing) {
console.log(Hello + thing);
}
// this:
hello(world)
// desugars to:
hello.call(window, world);
但ECMAScript5规范对此作了进一步限定,只在严格模式下才会将thisValue绑定到window,而一般情况下,绑定到undefined。
原文:a function invocation like fn(...args) is the same as fn.call(window [ES5-strict: undefined], ...args).
// this:
hello(world)
// desugars to:
hello.call(undefined, world);
内联函数(inline function):(function() {})() is the same as(function() {}).call(window [ES5-strict: undefined)
2 成员函数
2.1 内置成员函数
var person = {
name: Brendan Eich,
hello: function(thing) {
console.log(this + says hello + thing);
}
}
// this:
person.hello(world)
// desugars to this:
person.hello.call(person, world);
2.2 外成员函数
function hello(thing) {
console.log(this + says hello + thing);
}
person = { name: Brendan Eich }
person.hello = hello;
person.hello(world) // still desugars to person.hello.call(person, world)
hello(world) // [object DOMWindow]world
3 使用Ftotype绑定
将this绑定到指定对象的方法:
var person = {
name: Brendan Eich,
hello: function(thing) {
console.log( + says hello + thing);
}
}
var boundHello = function(thing) { return person.hello.call(person, thing); }
boundHello(world);
上述方法一般化:
var bind = function(func, thisValue) {
return function() {
return func.apply(thisValue, arguments);
}
}
var boundHello = bind(person.hello, person);
boundHello(world) // Brendan Eich says hello world
ECMAScript
您可能关注的文档
最近下载
- 17J008 挡土墙(重力式、衡重式、悬臂式)(最新).pdf VIP
- 雨污分流改造工程施工组织设计方案.pdf VIP
- 第九版儿科学配套课件-6.2-正常足月儿和早产儿的特点与护理.ppt VIP
- T_CACM 1601-2024 笑病中医诊疗指南.docx VIP
- 矿山三级(二级)矿量管理办法.doc VIP
- T_CACM 1583-2024 百合病中医诊疗指南.pdf VIP
- 趣味生物知识竞赛100选择题附答案.pdf VIP
- T_CACM 1584-2024 卑惵中医诊疗指南.pdf VIP
- 2025年第二批陕西延长石油集团所属单位内部遴选及选聘81人笔试参考题库附带答案详解.docx
- T_CACM 1585-2024 产后神志异常中医诊疗指南.pdf VIP
文档评论(0)