Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
您可以在Javascript中创建一个对象而不声明每个值吗?如何创建?_Javascript_Variables_Object_Declaration - Fatal编程技术网

您可以在Javascript中创建一个对象而不声明每个值吗?如何创建?

您可以在Javascript中创建一个对象而不声明每个值吗?如何创建?,javascript,variables,object,declaration,Javascript,Variables,Object,Declaration,我想做以下工作: var Room = function(name, north, south, east, west) { this.name = name; this.north = north; this.south = south; this.west = west; this.east = east; } 那里的北、南、东、西只是一面旗帜,上面写着那里是否有门 创建对象时,我必须执行以下操作: var bedroom = new Room('B

我想做以下工作:

var Room = function(name, north, south, east, west) {
    this.name = name;
    this.north = north;
    this.south = south;
    this.west = west;
    this.east = east;
}
那里的北、南、东、西只是一面旗帜,上面写着那里是否有门

创建对象时,我必须执行以下操作:

var bedroom = new Room('Bedroom', true, false, false, false);
我想知道是否有一种方法可以让我说出哪些方向具有真正的值,例如:

var bedroom = new Room({
    name: 'Bedroom',
    north: true
});

南、东和西变为假。这样,如果我要说20个不同的选项,我只需要指定我关心的选项,而不是有一个包含大量假值和“”值的长声明,我不需要记住它们的顺序(北、南、东、西?还是北、东、南、西?),

如果您真的需要为
西
单独使用实例变量,我建议您使用类似的方法,只需在空格分隔的字符串中传递您关心的方向。这使得调用代码更加简单,因为您不必为d传递任何内容不存在的方向

var Room = function(name, directions) {
    this.name = name;
    this.north = this.south = this.west = this.east = false;
    directions = directions.split(" ");
    directions.forEach(function(item) {
        this[item] = true;
    })
}

var bedroom = new Room('Bedroom', 'north south');

或者,您的对象方法也很好:

var Room = function(name, directions) {
    this.name = name;
    this.north = this.south = this.west = this.east = false;
    for (var dir in directions) {
        this[dir] = directions[dir];
    }
}

var bedroom = new Room('Bedroom', {north: true, south: true});

第三种方法是使用标志(通常在C/C++中使用)。这允许您在单个值中传递任意数量的标志:

var Room = function(name, directions) {
    this.name = name;
    this.directions = directions;
}

Room.prototype = {
   isNorth: function() { return this.directions & Room.north},
   isSouth: function() { return this.directions & Room.south},
   isWest: function() { return this.directions & Room.west},
   isEast: function() { return this.directions & Room.east},
};

Room.north = 1;
Room.south = 2;
Room.east = 4;
Room.west = 8;

var bedroom = new Room('Bedroom', Room.north | Room.south);
JavaScript对象 使用
| |
运算符添加默认值

var Room = function(name, north, south, east, west) {
    this.name = name;
    this.north = north || false;
    this.south = south || false;
    this.west = west || false;
    this.east = east || false;
}

这将告诉它,如果为其提供了数据,则使用
east
;如果
east
被证明是
未定义的
,,则使用false。您可以使用
|
操作符

var Room = function(name, north, south, east, west) {
    this.name = name;
    this.north = north || false;
    this.south = south || false;
    this.west = west || false;
    this.east = east || false;
}
e、 g


然后,当您调用
var r=new Room(“something”,{north:true});
时,所有其他值都将设置为false。

您可以使用某种子类

在main类中放置所有默认值

function AbstractRoom(name, north, south, east, west){
    this.name = name;
    this.north = north;
    this.south= south;
    this.east= east;
    this.west= west;
}
在你的子类中,你把你的价值观

function RoomNorth(name){
  AbstractRoom.call(this, name, true, false, false, false);
}
在这种情况下

var myNorthRoom = new RoomNorth('AGoodName');
在哪里

myNorthRoom.north关于:

var Room = function( name, directions ) {
    this.name = name;
    this.north = directions.north || false;
    this.south = directions.south || false;
    this.west = directions.west || false;
    this.east = directions.east || false;
}
然后调用构造函数,如:

var bedroom = new Room( 'Bedroom', { 'north': true } );
如果不定义方向,| |运算符将使其为false。

可以使用“for..in…”循环使用参数迭代输入对象。示例:

Room = function(options) {
    for (var key in options) {
        this[key] = options[key];
    }
}

var bedroom = new Room({
    name: 'Bedroom',
    north: true
});

下面是使用“链接”模式的另一种方法(它本身不是构建器):

有些人可能不喜欢这种模式,尤其是这种实现,因为它将getter/setter的返回类型更改为“有时可链接”和“有时返回值”,就像某些jQuery方法(例如
$.val


它还要求/鼓励方向(如“北”)可以通过getter方法访问。定义属性不允许使用这种链接语法。

我认为问题的关键是不必在构造函数中同时传递所有方向。这似乎有点愚蠢。使用这种方法将默认值指定为
true
false
。这是非常有用的将在这个方向上工作,但你不能用这种方式指定带有真实默认值的假值。如果北和南都应该是真的呢?@j08691-我最初没有意识到这是一个要求。我已经修改了我的答案以支持它。空格分隔的字符串有点特殊(也就是说,现在你在很多其他情况下都是这样做的),但在这种情况下,它有点可爱。谢谢,在尝试了其他情况后,这一个似乎对我最适合!@jonathanownchute-我添加了第三个版本,它使用二进制标志。不建议使用
for(基数中的var方向)在Javascript中迭代数组
由于这将迭代数组的所有属性,不仅是数组元素,还可以包括添加到数组对象的任何可枚举的polyfill方法或自定义方法。相反,请使用
.forEach()
或样式
for(var i=0;i
。同意!不确定我最初为什么使用这种方式;心不在焉?我已经为子孙后代更新了我的示例。我想一旦
for..of..
到处都受支持,我就会切换到这种方式。我喜欢任何其他值都
未定义的方式,但使用
for..
有问题。
var bedroom = new Room( 'Bedroom', { 'north': true } );
Room = function(options) {
    for (var key in options) {
        this[key] = options[key];
    }
}

var bedroom = new Room({
    name: 'Bedroom',
    north: true
});
function Room (name) {
   this.name = name;
}

Room.prototype = {
   north: function (v) {
       if (arguments.length > 0 } { this._north = v; return this; }
       else { return this._north; }
   },
   south: function (v) {
       if (arguments.length > 0 } { this._south = v; return this; }
       else { return this._south; }
   },
   // .. rest of getter-setters
   // The prototype can be used for defaults and/or lifting
   _north: false,
   _south: false,
   // .. rest of defaults
}

var room = new Room("bedroom")
  .north(true)
  .south(true); // => the Room object

var hasNorth = room.north(); // => true