JavaScript/new.target

来自康健生活

引言

new

使用构造函数时,使用 new 操作符进行调用,观察 this 指向。

使用 new

this 指向的是新创建的对象

function User(name) {
  this.name = name;
  console.log(this);
}
var foo = new User("Bob");
console.log(foo);
User {name: "Bob"}
User {name: "Bob"}

这里添加 return 没有意义。

function User(name) {
  console.log(this);
  return this.name = name;
}
var foo = new User("Bob");
console.log(foo);
User {}
User {name: "Bob"}

不使用 new

this 遵循普通函数的 this 定义(比如在浏览器中指向 window 对象)。

function User(name) {
  this.name = name;
  console.log(this);
}
var foo = User("Bob");
console.log(foo);
Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
undefined

使用 return 。

function User(name) {
  console.log(this);
  return this.name = name;
}
var foo = User("Bob");
console.log(foo);
Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
Bob

修复

function User(name) {
  console.log(this);
  if (!(this instanceof User)) {
    return new User(name);
  }
  this.name = name;
}
var foo = User("Bob");
console.log(foo);
Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
User {}
User {name: "Bob"}

new.target

new 调用,则函数内 new.target 指向构造函数;否则其值为 undefined。

function User(name) {
  console.log(this);
  if (!new.target) {
    return new User(name);
  }
  this.name = name;
}
var foo = User("Bob");
console.log(foo);
Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
User {}
User {name: "Bob"}

bind

普通函数 bind 操作,可以返回一个绑定的固定 this 的新函数。

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);
{age: "18", name: "Bob"}
ƒ User(name) {
  this.name = name;
  console.log(this);
}
undefined

构造函数则不同。bind 操作不能改变构造函数内的 this。

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);
User {name: "Bob"}
ƒ User(name) {
  this.name = name;
  console.log(this);
}
User {name: "Bob"}

新的 age 属性为什么没有添加上去呢

function User(name) {
  this.name = name;
  console.log(this);
}
const foo = User.bind({age: '18'});
const bar = foo('Bob');
{age: "18", name: "Bob"}
function User(name) {
  this.name = name;
  console.log(this);
}
const foo = User.bind({age: '18'});
const bar = new foo('Bob');
{name: "Bob"}

我的理解是 age: "18" 不是没添加上去,只是它存在于 User.bind({age: '18'}) 这个操作中的 User 对象属性上。

而 new 操作符复制新建了一个新的 User 函数,这里 new User 与原来的 User 的引用地址并不相同。(这里区别于 var a={};var b=a;b.c=2;console.log(a); 的普通对象复制操作,普通对象的复制操作是赋予其相同的引用地址。)

验证

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');
User {name: "Bob"}
User {name: "Bob"}
{age: "18", name: "Lili"}

顺序是User.bind({age: '18'})将 User 函数对象在栈空间存储的指向堆空间的引用地址所指向的被系统分配的堆空间上的内容进行了更改。

new foo('Bob')是初始化了 User 函数对象,并重新分配了栈空间并存储了一个指向存储 User 初始化内容的堆空间的引用地址。而原指向对内存的引用地址(foo 的引用地址)在没被回收之前仍然存在。

这里又涉及一个堆栈空间回收的问题。如:

var a = {n:1};
var b = a;
a.x = a;
a.x = {n:2};
a = {n:2};
console.log(a);
console.log(b);
{n: 2}
{n: 1, x: {…}}

a = {n:2} 这里 a 被重新赋值并分配了新的引用地址,根据栈的后入先出特性,新分配的引用地址压入栈中,如果旧的引用地址失去指向,后期会被回收。 但同时旧地址还在被 b 所使用,所以并不会回收。

那构造函数到底能不能用 bind 呢,

function User(name) {
  this.name = name;
  console.log(this);
}
const bar = new User('Bob');
bar.bind({addr: 'test addr'});
VM407:1 Uncaught TypeError: bar.bind is not a function
    at <anonymous>:1:5

验证类型

typeof bar;
bar.constructor;
Object.prototype.toString.call(bar);
typeof User;
User.constructor;
Object.prototype.toString.call(User);
"object"
ƒ User(name) {
  this.name = name;
  console.log(this);
}
"[object Object]"
"function"
ƒ Function() { [native code] }
"[object Function]"

可以看到 bar 和 User 的本质区别一个是函数一个是函数对象。所以不能说 new 出来的构造函数不能使用 bind,而是生成的构造函数的 object 类型不能使用 bind。

模拟实现

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;
};

(来源摘自互联网示例,作者 未知)

这个示例不适用与构造函数对象,所以进行更改。