JavaScript/new.target:修订间差异
来自康健生活
| 第124行: | 第124行: | ||
User {name: "Bob"} | User {name: "Bob"} | ||
</pre>}}}} | </pre>}}}} | ||
新的 age 属性为什么没有添加上去呢 | |||
{{leftrightflex|<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>}}}} | |||
{{leftrightflex|<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> 的普通对象复制操作,普通对象的复制操作是赋予其相同的引用地址。) | |||