你能在Javascript中创建一个对象而不声明每个值吗,以及如何
Can you create an object in Javascript without declaring every value, and how?
我想执行以下操作:
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 个不同的选项,我只需要指定我关心的选项,而不是用很多 false 和 '' 值进行长声明,我不需要记住它们应该在哪个顺序(北、南、东、西?或北、东、东、 南,西?
如果你真的需要单独的实例变量来north
、south
、east
和west
,我会建议这样的东西,你可以在空格分隔的字符串中只传递你关心的方向。 这使得调用代码变得更加简单,因为您不必为不存在的方向传递任何内容。
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
如果为它提供了数据,或者如果结果证明undefined
east
则使用 false 。
var Room = function (options) {
this.name = options.name;
var cardinals = ['north', 'south', 'east', 'west'];
// Check your directions param for each of the cardinal directions
// that you want to allow
cardinals.forEach(function (direction) {
if (typeof options[direction] !== 'undefined') {
this[direction] = options[direction];
} else {
this[direction] = false;
}
});
};
var room = new Room({ name: 'Bedroom', north: true, south: true });
您可以使用"for ..在..."循环以使用参数迭代输入对象。例:
Room = function(options) {
for (var key in options) {
this[key] = options[key];
}
}
var bedroom = new Room({
name: 'Bedroom',
north: true
});
||
运算符
例如
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 r = new Room("something", {north:true});
时,所有其他值都将设置为 false。
您可以使用某种子类。
在主类中,您输入所有默认值。
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 <-- true
myNorthRoom.south <-- false
怎么样:
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 } );
如果不定义方向,|| 运算符将使其为假。
这是使用"链接"模式的另一种方法(它本身不是构建器(:
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
有些人可能不喜欢这种模式 - 尤其是这种实现 - 因为它将getter/setter的返回类型更改为"有时可链接"和"有时返回值",就像一些jQuery方法(例如。 $.val
(。
它还要求/鼓励方向(例如。"北"(通过 getter 方法访问。定义属性不允许使用此类链接语法。
- 声明JavaScript选项对象的正确方法
- 声明Javascript名称空间的两种方法之间的区别
- 声明 JavaScript 变量
- 解析函数声明 Javascript
- 在 Rails 3 中声明 Javascript 变量时出错
- 我应该在对象原型中声明javascript对象方法吗
- 淘汰高级声明javascript"用“-喜欢
- 动态编辑html声明javascript
- 在构造函数和原型中声明javascript对象方法
- 声明javascript对象时在其变量中引用该对象
- 在typescript中声明javascript命名空间
- 声明javascript对象属性问题
- 在HTML链接中声明JavaScript变量
- 声明Javascript变量的所有可能方法
- 声明JavaScript成员函数的最佳实践
- 如何在ios中声明javascript
- 有效地声明JavaScript变量
- 以更简洁的方式声明javascript静态方法
- 使用function-prototype声明Javascript命名空间
- 如何在对象中正确声明Javascript对象数组