JavaScript/new.target:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” |
|||
| (未显示同一用户的12个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
== new == | == new == | ||
使用构造函数时,使用 new | 使用构造函数时,使用 new 操作符进行调用,观察 this 指向。 | ||
=== 使用 new === | |||
this 指向的是新创建的对象 | this 指向的是新创建的对象 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
function User(name) { | function User(name) { | ||
this.name = name; | this.name = name; | ||
| 第17行: | 第18行: | ||
这里添加 return 没有意义。 | 这里添加 return 没有意义。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
function User(name) { | function User(name) { | ||
console.log(this); | |||
return this.name = name; | return this.name = name; | ||
} | } | ||
var foo = new User("Bob"); | var foo = new User("Bob"); | ||
console.log(foo);</syntaxhighlight>|{{consoleout|<pre> | console.log(foo);</syntaxhighlight>|{{consoleout|<pre> | ||
User {} | |||
User {name: "Bob"} | User {name: "Bob"} | ||
</pre>}}}} | </pre>}}}} | ||
=== 不使用 new === | |||
this 遵循普通函数的 this 定义(比如在浏览器中指向 window 对象)。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
{{ | |||
function User(name) { | function User(name) { | ||
this.name = name; | this.name = name; | ||
| 第41行: | 第44行: | ||
使用 return 。 | 使用 return 。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
function User(name) { | function User(name) { | ||
console.log(this); | console.log(this); | ||
| 第51行: | 第54行: | ||
Bob | Bob | ||
</pre>}}}} | </pre>}}}} | ||
==== 修复 ==== | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
console.log(this); | |||
if (!(this instanceof User)) { | |||
return new User(name); | |||
} | |||
this.name = name; | |||
} | |||
var foo = User("Bob"); | |||
console.log(foo);</syntaxhighlight>|{{consoleout|<pre> | |||
Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …} | |||
User {} | |||
User {name: "Bob"} | |||
</pre>}}}} | |||
== new.target == | |||
new 调用,则函数内 new.target 指向构造函数;否则其值为 undefined。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
console.log(this); | |||
if (!new.target) { | |||
return new User(name); | |||
} | |||
this.name = name; | |||
} | |||
var foo = User("Bob"); | |||
console.log(foo);</syntaxhighlight>|{{consoleout|<pre> | |||
Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …} | |||
User {} | |||
User {name: "Bob"} | |||
</pre>}}}} | |||
== bind == | |||
普通函数 [https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind bind] 操作,可以返回一个绑定的固定 this 的新函数。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
const foo = User.bind({age: '18'}); | |||
const bar = foo('Bob'); | |||
console.log(foo); | |||
console.log(bar);</syntaxhighlight>|{{consoleout|<pre> | |||
{age: "18", name: "Bob"} | |||
ƒ User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
undefined | |||
</pre>}}}} | |||
构造函数则不同。bind 操作不能改变构造函数内的 this。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
const foo = User.bind({age: '18'}); | |||
const bar = new foo('Bob'); | |||
console.log(foo); | |||
console.log(bar);</syntaxhighlight>|{{consoleout|<pre> | |||
User {name: "Bob"} | |||
ƒ User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
User {name: "Bob"} | |||
</pre>}}}} | |||
新的 age 属性为什么没有添加上去呢 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
const foo = User.bind({age: '18'}); | |||
const bar = foo('Bob');</syntaxhighlight>|{{consoleout|<pre> | |||
{age: "18", name: "Bob"} | |||
</pre>}}}} | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
const foo = User.bind({age: '18'}); | |||
const bar = new foo('Bob');</syntaxhighlight>|{{consoleout|<pre> | |||
{name: "Bob"} | |||
</pre>}}}} | |||
我的理解是 <code>age: "18"</code> 不是没添加上去,只是它存在于 <code>User.bind({age: '18'})</code> 这个操作中的 <code>User</code> 对象属性上。 | |||
而 <code>new</code> 操作符复制新建了一个新的 <code>User</code> 函数,这里 <code>new User</code> 与原来的 <code>User</code> 的引用地址并不相同。(这里区别于 <syntaxhighlight lang="javascript" inline>var a={};var b=a;b.c=2;console.log(a);</syntaxhighlight> 的普通对象复制操作,普通对象的复制操作是赋予其相同的引用地址。) | |||
验证 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
const foo = User.bind({age: '18'}); | |||
const bar = new foo('Bob'); | |||
console.log(bar); | |||
const puz = foo('Lili');</syntaxhighlight>|{{consoleout|<pre> | |||
User {name: "Bob"} | |||
User {name: "Bob"} | |||
{age: "18", name: "Lili"} | |||
</pre>}}}} | |||
顺序是User.bind({age: '18'})将 User 函数对象在栈空间存储的指向堆空间的引用地址所指向的被系统分配的堆空间上的内容进行了更改。 | |||
new foo('Bob')是初始化了 User 函数对象,并重新分配了栈空间并存储了一个指向存储 User 初始化内容的堆空间的引用地址。而原指向对内存的引用地址(<code>foo</code> 的引用地址)在没被回收之前仍然存在。 | |||
这里又涉及一个堆栈空间回收的问题。如: | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
var a = {n:1}; | |||
var b = a; | |||
a.x = a; | |||
a.x = {n:2}; | |||
a = {n:2}; | |||
console.log(a); | |||
console.log(b);</syntaxhighlight>|{{consoleout|<pre> | |||
{n: 2} | |||
{n: 1, x: {…}} | |||
</pre>}}}} | |||
a = {n:2} 这里 a 被重新赋值并分配了新的引用地址,根据栈的后入先出特性,新分配的引用地址压入栈中,如果旧的引用地址失去指向,后期会被回收。 | |||
但同时旧地址还在被 b 所使用,所以并不会回收。 | |||
那构造函数到底能不能用 bind 呢, | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
function User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
const bar = new User('Bob'); | |||
bar.bind({addr: 'test addr'}); | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
VM407:1 Uncaught TypeError: bar.bind is not a function | |||
at <anonymous>:1:5 | |||
</pre>}}}} | |||
验证类型 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof bar; | |||
bar.constructor; | |||
Object.prototype.toString.call(bar); | |||
typeof User; | |||
User.constructor; | |||
Object.prototype.toString.call(User); | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
"object" | |||
ƒ User(name) { | |||
this.name = name; | |||
console.log(this); | |||
} | |||
"[object Object]" | |||
"function" | |||
ƒ Function() { [native code] } | |||
"[object Function]" | |||
</pre>}}}} | |||
可以看到 bar 和 User 的本质区别一个是函数一个是函数对象。所以不能说 new 出来的构造函数不能使用 bind,而是生成的构造函数的 object 类型不能使用 bind。 | |||
=== 模拟实现 === | |||
<syntaxhighlight lang="javascript"> | |||
Function.prototype.bind = function(context){ | |||
if (typeof this !== "function") { | |||
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); | |||
}; | |||
var args = Array.prototype.slice.call(arguments, 1), | |||
F = function(){}, | |||
self = this, | |||
bound = function(){ | |||
var innerArgs = Array.prototype.slice.call(arguments); | |||
var finalArgs = args.concat(innerArgs); | |||
return self.apply((this instanceof F ? this : context), finalArgs); | |||
}; | |||
F.prototype = self.prototype; | |||
bound.prototype = new F(); | |||
return bound; | |||
};</syntaxhighlight> | |||
{{unknown}} | |||
这个示例不适用与构造函数对象,所以进行更改。 | |||