-1
var veryLongObjectName = {};
// ...


if (!veryLongObjectName["verylongpropertyName"]) 
      veryLongObjectName["verylongpropertyName"] = 0;

if (veryLongObjectName["verylongpropertyName"]++) 
      console.log("I've seen it already");

我想通过更改最后两行来使上面的代码不那么冗长;我怎样才能做到这一点?

4

3 回答 3

2

如何使用参考。

var veryLongObjectName = {};
var prop = veryLongObjectName["verylongpropertyName"];

if (!prop) 
    prop = 0;

if (prop++) 
    console.log("I've seen it already");
于 2013-07-08T20:55:17.287 回答
2

如果它们打扰您,您为什么不为您的对象和道具制作简写:

var prop = 'longpropertyname';
var obj = longObjectName;
// do whatever you want with obj[prop]
于 2013-07-08T20:59:07.983 回答
1
var veryLongObjectName = {};
var obj = veryLongObjectName;
var prop = "verylongpropertyName";

if (!obj[prop]) obj[prop] = 0;

if (obj[prop]++) console.log("I've seen it already");

或创建一个函数来处理您想要完成的事情:

function checkProp(obj, prop) {
    obj[prop] = obj[prop] || 0;
    if (obj[prop]++) console.log("I've seen it already");
}
于 2013-07-08T21:02:49.783 回答