JavaScript中的新关键字在第一次遇到时可能会非常令人困惑,因为人们倾向于认为JavaScript不是一种面向对象的编程语言。

这是怎么一回事?它解决了什么问题?什么时候合适,什么时候不合适?


当前回答

有时代码比文字更容易:

var func1 = function (x) { this.x = x; }                   // Used with 'new' only
var func2 = function (x) { var z={}; z.x = x; return z; }  // Used both ways
func1.prototype.y = 11;
func2.prototype.y = 12;

A1 = new func1(1);  // Has A1.x  AND  A1.y
A2 =     func1(1);  // Undefined ('this' refers to 'window')
B1 = new func2(2);  // Has B1.x  ONLY
B2 =     func2(2);  // Has B2.x  ONLY

对我来说,只要我不做原型,我就使用func2的样式,因为它给了我函数内外更多的灵活性。

其他回答

除了丹尼尔·霍华德的回答之外,以下是new的作用(或者至少看起来是这样):

function New(func) {
    var res = {};
    if (func.prototype !== null) {
        res.__proto__ = func.prototype;
    }
    var ret = func.apply(res, Array.prototype.slice.call(arguments, 1));
    if ((typeof ret === "object" || typeof ret === "function") && ret !== null) {
        return ret;
    }
    return res;
}

虽然

var obj = New(A, 1, 2);

相当于

var obj = new A(1, 2);

new关键字用于创建新的对象实例。是的,JavaScript是一种动态编程语言,它支持面向对象的编程范式。关于对象命名的约定是:对于应该由new关键字实例化的对象,始终使用大写字母。

obj = new Element();

它有三个阶段:

1.创建:它创建一个新对象,并将此对象的[[prototype]]属性设置为构造函数的prototype属性。

2.执行:它指向新创建的对象并执行构造函数。

3.返回:在正常情况下,它将返回新创建的对象。但是,如果显式返回非空对象或函数,则会返回此值。需要提及的是,如果返回非空值,但它不是对象(例如Symbol value、undefined、NaN),则忽略该值并返回新创建的对象。

function myNew(constructor, ...args) {
  const obj = {}
  Object.setPrototypeOf(obj, constructor.prototype)
  
  const returnedVal = constructor.apply(obj, args)
  
  if (
    typeof returnedVal === 'function' 
    || (typeof returnedVal === 'object' && returnedVal !== null)) {
      return returnedVal
  }
  return obj
}

有关myNew的更多信息和测试,请阅读我的博客:https://medium.com/@magenta2127/how-does-the-new-operator-work-f7eaac692026

所以它可能不是为了创造对象的实例

它正是为了这个。您可以这样定义函数构造函数:

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

var john = new Person('John');

然而,ECMAScript的额外好处是您可以使用.prototype属性进行扩展,因此我们可以执行以下操作。。。

Person.prototype.getName = function() { return this.name; }

从该构造函数创建的所有对象现在都将有一个getName,因为它们可以访问原型链。

new关键字使用函数作为构造函数创建对象的实例。例如:

var Foo = function() {};
Foo.prototype.bar = 'bar';

var foo = new Foo();
foo instanceof Foo; // true

实例继承自构造函数的原型。因此,在上面的例子中。。。

foo.bar; // 'bar'