You Don't Know JS: this & Object Prototypes( 第一章 this or That?)
Foreword
this 关键字和prototypes
他们是用JS编程的基础。没有他们创建复杂的JS程序是不可能的。
我敢说大量的web developers从没有建立过JS Object,仅仅对待这门语言作为一个事件绑定胶水,在按钮和Ajax请求之间。
我也曾是他们的一员,但是当我了解到如何掌握prototypes并创建JS对象,一个世界的可能性对我打开了。
- Chapter 1: this Or That?
- Chapter 2: this All Makes Sense Now!
- Chapter 3: Objects
- Chapter 4: Mixing (Up) "Class" Objects
- Chapter 5: Prototypes
- Chapter 6: Behavior Delegation
- Appendix A: ES6 class
- Appendix B: Thank You's!
Chapter 1: this Or That?
this作为一个识别符号关键字自动定义在每个函数的作用域中,但是它究竟涉及什么,甚至困扰着有经验的JS developer。
Why this?
既然很难理解,为什么还使用?它的麻烦大于它的价值吗?
看案例:
function identify() {
return this.name.toUpperCase();
}
var me = {
name: "Kyle"
};
var you = {
name: "Reader"
};
identify.call( me ); // KYLE
identify.call( you ); // READER
这段代码让identify函数被多次调用。
如果不用this关键字,也可以通过传入一个context对象来实现:
function identify(context) {
return context.name.toUpperCase();
}
identify( you ); // READER
由此可见,this机制提供了更优雅的方式,暗示传入了一个对象,这让API设计更清晰,复用更容易。
Confusions
首先,消除一些错误观念。常常有2个错误的假设:
Itself
假设this是函数自身。
function foo(num) {
console.log( "foo: " + num );
// keep track of how many times `foo` is called
this.count++;
}
foo.count = 0;
var i;
for (i=0; i<10; i++) {
if (i > 5) {
foo( i );
}
}
// foo: 6
// foo: 7
// foo: 8
// foo: 9
// how many times was `foo` called?
console.log( foo.count ); // 0 -- WTF?
因为this指向了window对象。this指向的是调用函数的对象。
//在call方法内加上函数对象的identifier名字
for (var i=0; i<10; i++) {
if (i > 5) {
foo.call( foo, i );
}
} // how many times was `foo` called?
console.log( foo.count ); //
除此之外,还有规避this的方法。但都没有直面this到底是什么的问题。
- 使用foo.count代替this.count.
- 单独声明一个data对象{ count: 0},用于计数 data.count++
Its Scope
另一个常见的误区是关于this,它涉及了函数的作用域。这里有些诡计:
在某些情况下是对的,但在其他情况下,这么理解是受到误导的!
this关键字,在任何时候,都不涉及一个function的 Lexical scope
function foo() {
var a = 2;
this.bar();
}
function bar() {
console.log( this.a );
}
foo(); //undefined
写 这段代码的人,试图通过this关键字,把变量a传递给bar函数。以为this是桥梁,能在两个函数的lexical scope之间传递内部变量a。 ❌。没有这个桥梁!!
this不能得到foo函数的Lexical scope!!
this和Lexical scope是不相关的!!!
What's this?
this机制到底是如何工作的?
this和函数调用的方式相关!
当一个函数被invoked,一条记录被创建。这条记录包含信息:
- 函数从哪里调用(the call-stack)
- 函数如何被invoked
- 什么参数被传入。
这条记录的属性之一就是 this 引用。它会在函数的执行期间被使用。
下一章,我们会学习寻找一个函数的call-site(调用点),来决定它的执行如何绑定this关键字
Review
this既不是函数自身的引用也不是函数的lexical作用域的引用!!
this实际是一个在函数被invoked时创建的绑定binding!!!
this引用什么由函数被调用的地点call-site决定!!!
You Don't Know JS: this & Object Prototypes( 第一章 this or That?)的更多相关文章
- You Don't Know JS: this & Object Prototypes (第6章 Behavior Delegation)附加的ES6 class未读
本章深挖原型机制. [[Prototype]]比类更直接和简单! https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%2 ...
- You Don't Know JS: this & Object Prototypes( 第5章 Prototypes)
qu上章提到过[[prototype]] chain, 本章详细分析 ⚠️所有试图模仿类复制的行为,如上章提到的mixins的变种,完全规避了[[Prototype]] chain机制,本章会谈到这方 ...
- You Don't Know JS: this & Object Prototypes( 第4章 Mixing "Class" Objects)
本章移到“Object oriented programming”和"classes". 看‘class orientation‘ 的设计模式: instantiation, in ...
- You Don't Know JS: this & Object Prototypes( 第2章 this)
this is a binding made for each function invocation, based entirely on its call-site (how the functi ...
- You Don't Know JS: this & Object Prototypes( 第3章 对象)
前2章探索了this绑定指向不同的对象需要函数引用的call-site. 但是什么是对象,为什么我们需要指向它们? 本章探索细节. Syntax the rules that describe ho ...
- You Don't Know JS: Async & Performance(第一章, 异步:now & later)
Chapter 1: Asynchrony: Now & Later 在一门语言中,比如JavaScript, 最重要但仍然常常被误解的编程部分是如何在一个完整的时间周期表示和操作程序行为. ...
- JS的Object漫想:从现象到“本质”
转自:http://zzy603.iteye.com/blog/973649 写的挺好,用于记录,把对象分成概念的Object(var f={})和 类的Object(function F(){}) ...
- Javascript中Function,Object,Prototypes,__proto__等概念详解
http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...
- js中Object.defineProperty()和defineProperties()
在介绍js中Object.defineProperty()和defineProperties()之前,我们了解下js中对象两种属性的类型:数据属性和访问器属性. 数据属性 数据属性包含一个数据的位置, ...
随机推荐
- SSM集成activiti6.0错误集锦(二)
项目环境 Maven构建 数据库:Orcle12c 服务器:Tomcat9 <java.version>1.8</java.version> <activiti.vers ...
- TFS 报错解决方案:tf400324
同事的解决方案没报这个问题将他的C:\Windows\System32\drivers\etc\hosts文件覆盖自己的文件,主要备份自己的文件不行了替换掉
- Android 系统(64)---Android中m、mm、mmm、mma、mmma的区别【转】
本文转载自:https://blog.csdn.net/zhangbijun1230/article/details/80196379 Android中m.mm.mmm.mma.mmma的区别 m ...
- matlab练习程序(k-means聚类)
聚类算法,不是分类算法. 分类算法是给一个数据,然后判断这个数据属于已分好的类中的具体哪一类. 聚类算法是给一大堆原始数据,然后通过算法将其中具有相似特征的数据聚为一类. 这里的k-means聚类,是 ...
- php高级开发参考地址
高级开发 : http://www.cnblogs.com/bananaplan/p/The-Right-Way-For-PHPer.html
- 如何卸载旧版本的dotnet core
How to remove the .NET Core Runtime and SDK https://docs.microsoft.com/en-us/dotnet/core/versions/re ...
- 如何创建并运行java线程 , 多线程使用
http://www.importnew.com/20672.html https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblo ...
- Python有趣现象(不定时更新)
1.list中extend方法有趣现象 1.1 List+=Str 与 List.extend(Str) list1 = [11,2,45] str1 = 'Michael' list1.extend ...
- Mybatis工程搭建
工程搭建 • 1依赖包 • 2配置文件 • 2.1spring-mybatis.xml • 2.2mybatis-config.xml自带配置文件 • 2.3 mapper(dao)对象 • 2.4 ...
- HTML元素 绑定href属性
给<li>标签绑定href属性 <ul class="honor-list clearfix"> <li style="cursor:poi ...