用于初始化对象的编码模式 - 构造函数(new)与Object.create()(Crockford) [英] Coding patterns for initializing objects - constructors (new) vs. Object.create() (Crockford)

查看:66
本文介绍了用于初始化对象的编码模式 - 构造函数(new)与Object.create()(Crockford)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这不是关于经典和原型继承的问题。它是关于用于初始化对象的编码模式。类构造函数创建和初始化对象,而避免 new 运算符并转到 Object.create()只创建对象并建立原型链。我还没有找到一个在线资源,它解释了在进行Crockford Object.create()方法时进行创建和初始化的最佳实践编码模式。

Note: This is not a question about classical and prototype inheritance. It's about what coding patterns to use to initialize objects. Class constructors create and initialize objects, whereas avoiding the new operator and going for Object.create() only creates the object and sets up the prototype chain. I have not yet found an online resource that explains best-practice coding patterns for doing creation and initialization when going for the Crockford Object.create() approach.

如果我有一个构造函数(在我的脑海中这使我的课程,虽然我知道在主流JavaScript中技术上不存在类)

If I have a constructor (in my head this makes my class, although I know classes don't technically exist yet in mainstream JavaScript)

function Person(first, last) {
  this.name = {
    first: first,
    last: last
  };
}

Person.prototype.tellName = function() {
  return this.name.first + ' ' + this.name.last;
}

我可以像这样实例化它

var p1 = new Person('John', 'Doe');
var p2 = new Person('Sven', 'Svensson');

并更改 Person.name.first Person.name.last 单独

p1.tellName();  // Output: 'John Doe'
p2.tellName();  // Output: 'Sven Svensson'

p1.name.first = 'Peter';
p2.name.last = 'Celery';

并执行对象的函数 Person.tellName()具有以下输出

And execute the object's function Person.tellName() with the following output

p1.tellName();  // Output: 'Peter Doe'
p2.tellName();  // Output: 'Sven Celery'

这与我如何建立这样一个非常相似C ++或Java中的类。

Which is very similar to how I'd approach building such a class in C++ or Java.

例如

...
// some code that does the same stuff as defining a class + constructor
...

var p1 = ???????
var p2 = ???????

// The following is the code and behavior I'm looking to get
p1.tellName();  // Output: 'John Doe'
p2.tellName();  // Output: 'Sven Svensson'

p1.name.first = 'Peter';
p2.name.last = 'Celery';

p1.tellName();  // Output: 'Peter Doe'
p2.tellName();  // Output: 'Sven Celery'


推荐答案

而不是拥有:

function Person(first, last) {
  this.name = {
    first: first,
    last: last
  };
}

Person.prototype.tellName = function() {
  return this.name.first + ' ' + this.name.last;
}

您只需:

function Person(first, last) {
  return {
    name: { first: first, last: last },
    tellName: function() { return this.name.first + ' ' + this.name.last; }
  };
};

或者,如果您更喜欢 person.create()看起来,然后:

Or, if you prefer how person.create() looks, then:

var person = {
  create: function(first, last) {
    return {
      name: { first: first, last: last },
      tellName: function() { return this.name.first + ' ' + this.name.last; }
    };
  }
};

但在第二种情况下你会有一个不必要的对象( person )只包含一个函数( person.create())。

But in the second case you'd have an unnecessary object (person) containing only one function (person.create()).

不需要 Object.create new 因为那些是继承你说你不在乎关于。这可以让你这样做:

No need for Object.create nor new since those are for inheritance which you said you do not care about. This would let you do:

var p1 = Person('John', 'Doe');
var p2 = Person('Sven', 'Svensson');

一个有趣的事实是你仍然使用 person.create 前面如果你想这样,但它不起作用。如果 使用现有函数,则可以使用 .call 上下文c>

A fun fact is that you can still use new in front of person.create this way if you want but it would offer no effect. If you have to use the existing function you can set the this context explicitly by using .call

// with your original `Person`
var p1 = Person.call({}, 'John', 'Doe');
var p2 = Person.call({}, 'Sven', 'Svensson');

这不会设置原型,因为函数不像构造函数那样被调用。请参阅此答案,了解原型是什么答案是否有效 - 在一条线上它是关于共享功能它不是关于构建对象的属性。

This would not set the prototype either since the function is not called like a constructor. See this answer about what prototypical answer does and doesn't do - in a line it's about sharing functionality it's not about constructing properties of your objects.

这篇关于用于初始化对象的编码模式 - 构造函数(new)与Object.create()(Crockford)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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