【问题标题】:In Javascript how do I automatically set properties to null if they don't exist?在 Javascript 中,如果属性不存在,如何自动将其设置为 null?
【发布时间】:2015-09-06 12:02:47
【问题描述】:

我有一个 Javascript 类(使用 John Resig's approach),我创建了一个实例并传递了一个 args 对象,如下所示:

var myCucumber = new Cucumber({
   size: 'small'
   organic: true
})

在类本身中,它引用了args 对象的许多属性。但是,这些属性都不是强制性的,因此有时可能会缺少一些属性,这会导致“属性未定义”错误。

为了解决这个问题,我做了以下事情:

args.size = args.size || null;
args.organic = args.organic || false;
args.colour = args.colour || null;
args.origin = args.origin || null;

对于可能在整个课程中使用的每个属性都必须这样做似乎有点烦人。

如果在创建类的实例时没有传入 args 的任何属性,是否有一种简洁的方法可以假定为 null

【问题讨论】:

  • 无论你在哪里使用它,你不能只检查undefined,或者两者兼而有之,或者这是一个疯狂的想法,只检查任何虚假的东西吗?
  • 如果您确实想使用这些属性,只需在其构造函数中使用一些初始值(甚至为 null)对其进行初始化。

标签: javascript


【解决方案1】:

试试这样的:

for (var key in args.keys()) {
    args[key] = args[key] || null;
}

这是因为每个对象都有一个 keys() 函数,该函数返回该对象的键数组。

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

【讨论】:

    【解决方案2】:

    你有几种方法,但我不会使用 Resig 方法,因为它在 ES5 Is John Resig's Javascript inheritance snippet deprecated? 中有问题

    1) (Resig) 创建一个构造函数并为所有不存在的属性赋值:

    var Cucumber = Class.extend({
    { 
      init: function(args){
        this.size = null;
        this.organic = false;
        //etc
        for (var key in args.keys()) {
         this[key] = args[key];
        }
      },
    }
    

    2) 第二个选项使用带有描述符的Object.create。这使您能够使用默认值创建对象属性。

    // Example where we create an object with a couple of sample properties.
    // (Note that the second parameter maps keys to *property descriptors*.)
    o = Object.create(Object.prototype, {
      // foo is a regular 'value property'
      size: { writable: true, configurable: true, value: null },
    });
    

    3) 类似使用Object.defineProperty

    我更喜欢后面的两种方式,因为我相信使用 Object.create / Object.defineProperty 更清楚更好,这里有一些关于此事的附加信息:

    http://jaxenter.com/a-modern-approach-to-object-creation-in-javascript-107304.html

    Understanding the difference between Object.create() and new SomeFunction()

    【讨论】:

      【解决方案3】:

      您可以在引用它之前检查是否已经设置了任何对象属性,就像@adeneo 建议的那样。

      如果您的对象有很长的属性列表,您可以使用@aliasm2k 的解决方案。

      或者你可以编写一个对象构造函数并使用它。例如

      function ToothPaste(color = null, flavor = null, amount = null){
        this.color = color;
        this.flavor = flavor;
        this.amount = amount;
      }
      
      var myTp = new ToothPaste('white');
      alert(myTp.color);
      alert(myTp.flavor);
      alert(myTp.amount);
      

      【讨论】:

        【解决方案4】:

        我建议添加一个函数以按预期方式处理值。

        例子:

        Cucumber.prototype._args = function(attr) {
          return this.args[attr] || null;
        }
        
        // Then you may use it to access values as follows:
        this._args('size');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-12
          • 1970-01-01
          • 2012-04-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-24
          相关资源
          最近更新 更多