Javascript:关于如何定义新数据类型的一些指导原则是什么? [英] Javascript: what are some guidelines on how to define new data types?

查看:78
本文介绍了Javascript:关于如何定义新数据类型的一些指导原则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您正在创建数据类型并公开其行为。

Suppose you are creating the data type and expose its behavior.

您能举例说明您何时使用:

Can you give some examples of when would you use:


  • 一个功能& new:

  • a function & new:

// define new data type
var CustomDataType= function(){ 
                      this.a='whatever';
                      this.doX= function(){/*some code*/};
                    }

// create a new instance of our custom data type
var obj= new customDataType();


  • 一个对象文字& Object.create:

  • an object literal & Object.create:

    // define new data type
    var customDataType = {
                           a: 'whatever',
                           doX: function(){/*some code*/}
                         }
    
    // create a new instance of our custom data type
    var obj= Object.create(customDataType);
    


  • 构建对象的函数:

  • a function that builds your object:

    function customDataTypeFactory(options){
      return {
               a: 'whatever',
               doX: function(){/*some code*/}
             }
    };
    
    // create a new instance of our custom data type
    var obj= customDataTypeFactory(options);
    


  • 我觉得这可以标记为重复: new vs Object.create 但我的主要兴趣不在于讨论哪一个更好,而是知道是否有特定用例,其中一个应优先于其他用户。

    I feel this could be labeled duplicate for: new vs Object.create but my main interest is not in discussing which one is better but rather to know if there are specific use cases where one should be preferred over the others.

    我已经阅读了很多关于相关问题的帖子和来自Crockford的书:Javascript:好的部分。到目前为止,我已经得出结论,这是一个偏好的问题,很难Crockford的建议对我产生了很大的共鸣:尽量避免使用危险和不必要的功能......我在谈论 new

    I have read many posts on related questions and the book from Crockford: Javascript: the good parts. So far I have concluded that it is a matter of preference, tough the advice from Crockford resonates a lot with me to me: "try to avoid the features that are dangerous and unnecessary"... I'm talking about new.

    推荐答案

    我将从我通常定义类的方式开始:

    I'll start with the way I usually define classes:

    function CustomDataType(a)
    {
      this.a = a;
      this.b = 2;
    }
    CustomDataType.prototype = {
      doX : function () {}
    };
    var obj = new CustomDataType(1);
    

    我在构造函数中分配变量,因为 F.prototype = {a:[ 1,2,3]; } 是有问题的,除非在构造函数中重新初始化属性,否则数组将在实例之间共享(对于所有非基本类型都有问题)。当属性以某种方式依赖于构造函数的参数时,它也是必需的。我在原型中声明了方法,以便它们在所有实例之间共享,如果你使用继承,这意味着你可以进行超级调用,它也会导致更少的内存使用,因为该方法只需要分配一次(尽管可能不是这是一个大问题。)

    I assign variables in the constructor because F.prototype = { a : [1,2,3]; } is problematic, the array will be shared between instances unless the property is reinitialized in the constructor (problematic for all non-primitive types). And it's also needed when the property somehow depends on the arguments to the constructor. I declare methods in the prototype so that they are shared between all instances, if you use inheritance it means you can have super calls, it will also lead to less memory usage since the method only has to be allocated once (though that probably isn't a big issue).

    我从未使用过 Object.create ,我认为没有任何理由除非你遇到一些疯狂的动态hackery。

    I have never used Object.create, I don't see any reason to do to that unless you are into some insane dynamic hackery.

    每当感觉不必为对象创建一个单独的类时,我会使用对象文字,例如,如果它是参数一个方法(所以调用者不必显式地实例化某些东西),或者它是否是其他任何人都不应该访问的内部值。

    I use object literals whenever it feels unnecessary to create a separate class for the objects, for instance if it's arguments to a methods (so callers don't have to explicitly instantiate something), or if it's some internal values that shouldn't be accessed by anyone else.

    你的工厂本质上是与对象文字相同。我想如果你需要创建一些jsonesque结构并设置一些默认值,它会很有用。

    Your factory is essentially the same as the object literal. I suppose it can be useful if you need to create some jsonesque structure and set some default values.

    我不明白为什么 new 会是危险的还是不必要的,他说的是什么情况?

    I don't see why new would be dangerous or unnecessary, in what context did he say that?

    但我认为很多都归结为味道。保持一致,不要让事情复杂化。

    But I think a lot of it comes down to taste. Be consistent and don't complicate things.

    这篇关于Javascript:关于如何定义新数据类型的一些指导原则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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