有效的JavaScript:使您的构造函数功能与新的无关 [英] Effective JavaScript: Making your Constructor Function new-agnostic

查看:114
本文介绍了有效的JavaScript:使您的构造函数功能与新的无关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在阅读有效的JavaScript,我遇到了这个问题。

I've been reading 'Effective JavaScript' lately and I came across this question.

作者解释了如何使构造函数与新的无关,因为如果开发人员忘记使用'new'关键字调用Constructor,'this'指的是'窗口'。那讲得通。令我困惑的是他实施的目的。

The author explains how it's important to make your Constructor Function new-agnostic because if a developer forgets to call the Constructor with the 'new' keyword, 'this' refers to 'Window'. That makes sense. What's confusing me is the purpose of his implementation.

他建议像这样设置你的构造函数。

He advices to set up your constructor like this.

var Person = function(name, age){
  var that = this instanceof Person ? this : Object.create(Person.prototype);
  that.name = name;
  that.age = age;
  return that;
}

这是有道理的。您检查'this'是否是Person的实例,这意味着它是使用'new'关键字调用的。如果不是,则创建一个与this相同的新Object并返回该对象。

That makes sense. You check if 'this' is an instance of Person, meaning it was called with the 'new' keyword. If it's not, create a new Object that does the same thing as 'this' and return that object.

我的问题是这个。如果你正在设置一个与'this'完全相同的新Object,那么我们不能再担心是否通过前面的'this'调用了构造函数而只是创建了新对象。

My question is this. If you're setting up a new Object that does the same thing as 'this', can't we just never worry about if the constructor was called with new by foregoing 'this' and just creating the new object.

var Person = function(name, age){
  var that = Object.create(Person.prototype);
  that.name = name;
  that.age = age;
  return that;
}

为什么要担心'this'和'new'以及为什么不总是只是像上面那样创建我们的构造函数?

Why worry about 'this' and 'new' at all and why not always just create our constructors like the one above?

推荐答案


为什么要担心'this'和'new '总而言之,为什么不总是像上面那样创建我们的构造函数?

Why worry about 'this' and 'new' at all and why not always just create our constructors like the one above?

因为它只是更简洁的只写

Because it is just more concise to write only

function Person(name, age) {
    this.name = name;
    this.age = age;
}

是发明的在 Object.create 之前(在旧浏览器中不可用)并成为标准模式。大多数人都习以为常,他们不打算包含 if(!(this instanceof Person))返回新的Person(姓名,年龄) check。

new was invented before Object.create (which is not available in older browsers) and became the standard pattern. Most people are so accustomed to it that they don't bother to include a if (!(this instanceof Person)) return new Person(name, age) check.


如果你正在设置一个与'this'做同样事情的新Object,我们不能再担心构造函数被前面的'this'调用new并只创建新对象。

If you're setting up a new Object that does the same thing as 'this', can't we just never worry about if the constructor was called with new by foregoing 'this' and just creating the new object.

不,你并不总是知道如何创建新的对象。 此实例的Person 对于从 Person.prototype 继承的任何其他内容也是如此,并且允许类继承

No, you don't always know how to create the new object. this instanceof Person is also true for anything else that does inherit from Person.prototype, and does allow for class inheritance:

function Employee(name, age, salary) {
    Person.call(this, name, age);
    this.salary = salary;
}
Employee.prototype = Object.create(Person.prototype);

Person.call(this) wouldn如果你选择总是返回一个新对象,那就有可能。

The Person.call(this) wouldn't be possible if you chose to always return a new object.

这篇关于有效的JavaScript:使您的构造函数功能与新的无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆