本文介绍了这种编码JavaScript的风格有什么问题? (闭包与原型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在辩论如何在我们的JS应用程序中处理对象,研究Stoyan Stefanov的书,阅读新,这个,原型,闭包等等的无尽的SO帖子(事实上,有这么多,并且他们有这么多竞争的理论,建议没有完全明显的答案)。

We have been debating how best to handle objects in our JS app, studying Stoyan Stefanov's book, reading endless SO posts on 'new', 'this', 'prototype', closures etc. (The fact that there are so many, and they have so many competing theories, suggests there is no completely obvious answer).

所以让我们假设我们不关心私人数据。

So let's assume the we don't care about private data. We are content to trust users and developers not to mess around in objects outside the ways we define.

鉴于此,我们非常信任用户和开发人员,

Given this, what (other than it seeming to defy decades of OO style and history) would be wrong with this technique?

// namespace to isolate all PERSON's logic
var PERSON = {};

// return an object which should only ever contain data.
// The Catch: it's 100% public

PERSON.constructor = function (name) {
     return {
         name: name
     } 
}

// methods that operate on a Person
// the thing we're operating on gets passed in

PERSON.sayHello = function (person) {
     alert (person.name); 
}

var p = PERSON.constructor ("Fred");
var q = PERSON.constructor ("Me");

// normally this coded like 'p.sayHello()'
PERSON.sayHello(p);
PERSON.sayHello(q);

显然:


  1. 没有什么可以阻止某人在不公平的
    方式中突变'p',或者只是PERSON的逻辑结束了遍布整个地方。

  2. 这是一个小的麻烦,传递'p'到每个函数,你
    想使用它。

  3. 这是一种奇怪的方法

  1. There would be nothing to stop someone from mutating 'p' in unholyways, or simply the logic of PERSON ending up spread all over the place. (That is true with the canonical 'new' technique as well).
  2. It would be a minor hassle to pass 'p' in to every function that youwanted to use it.
  3. This is a weird approach.


b $ b

但是,有足够的理由解雇它吗?积极的一面:

But are those good enough reasons to dismiss it? On the positive side:


  1. 这是高效,反对关闭重复的函数声明。

  2. 看起来非常简单易懂,而不是用
    'this'无处不在。

  1. It is efficient, as (arguably) opposed to closures with repetitive function declaration.
  2. It seems very simple and understandable, as opposed to fiddling with'this' everywhere.

关键在于隐私权。我知道我会为此而被抨击,但是,寻找任何反馈。干杯。

The key point is the foregoing of privacy. I know I will get slammed for this, but, looking for any feedback. Cheers.

推荐答案

没有什么内在的错误。但它放弃了使用Javascript原型系统固有的许多优点。

There's nothing inherently wrong with it. But it does forgo many advantages inherent in using Javascript's prototype system.


  • 你的对象不知道任何关于自己的东西,文字。因此 instanceof 将无法帮助您确定其来源。

  • 您的方法本质上是命名空间的静态函数,您必须通过将对象作为第一个论据。通过拥有原型对象,您可以利用动态分发,使 p.sayHello()可以为 PERSON ANIMAL ,具体取决于Javascript知道的类型。这是一种形式的多态性。

  • 您实际上不需要构造函数
  • code>函数,因为函数已经是对象。 PERSON 变量也可以是构造函数。
  • Your object does not know anything about itself other than that it is an object literal. So instanceof will not help you to identify its origin. You'll be stuck using only duck typing.
  • Your methods are essentially namespaced static functions, where you have to repeat yourself by passing in the object as the first argument. By having a prototyped object, you can take advantage of dynamic dispatch, so that p.sayHello() can do different things for PERSON or ANIMAL depending on the type Javascript knows about. This is a form of polymorphism. Your approach requires you to name (and possibly make a mistake about) the type each time you call a method.
  • You don't actually need a constructor function, since functions are already objects. Your PERSON variable may as well be the constructor function.

这里是另一种模式,它保留了你所拥有的但提供了上述优点:

Here is another pattern that keeps what you have but supplies the above advantages:

function Person(name)
{
    var p = Object.create(Person.prototype);
    p.name = name; // or other means of initialization, use of overloaded arguments, etc.
    return p;
}
Person.prototype.sayHello = function () { alert (this.name); }

var p = Person("Fred"); // you can omit "new"
var q = Person("Me");
p.sayHello();
q.sayHello();
console.log(p instanceof Person); // true
var people = ["Bob", "Will", "Mary", "Alandra"].map(Person);
        // people contains array of Person objects

这篇关于这种编码JavaScript的风格有什么问题? (闭包与原型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:14