gpt4 book ai didi

javascript - 文字与原型(prototype)对象表示法的数据结构

转载 作者:行者123 更新时间:2023-11-28 08:39:30 25 4
gpt4 key购买 nike

我正在构建一个文本冒险来学习 javascript,但我不确定在使用对象原型(prototype)时如何定位对象。例如,使用文字语法,我定义了位置对象:

locations = {
yard : {
title: "Yard",
description: "You are in the Yard. You can go east.",
exits : {
north : -1,
east : "foyar",
south : -1,
west : -1
},
items: ["lamp"],
points: 5
},
// etc..
}

并且可以找到这样的信息(例如):

currentLocation = locations[currentLocation]["exits"][direction]

...但是当我从原型(prototype)定义对象时,我不太确定如何排列和定位对象:

function item(){
this.id = undefined;
this.title = "";
this.description = "";
this.points = 0;
this.canTake = false;
}

var glasses = new item();
glasses.id = 1;
glasses.title = "Glasses";
glasses.description = "A scratched up pair of glasses. They aren't your prescription";
glasses.points = 10;
glasses.canTake = true;

在这里构建和引用我的数据的最佳方式是什么? (在这种情况下使用文字或原型(prototype)方法有什么特别的优势吗?)

最佳答案

如果您有多个相同类型的对象实例(如 item 所示)并且它们具有行为,那么您将把该行为放在原型(prototype)上。这是因为它在实例之间共享,并且在创建/存储实例时可以节省 CPU 和内存。它还允许您通过继承来重新使用类似的项目逻辑(员工是一个人,因此姓名、年龄和性别可以由人初始化并由员工重新使用,而无需复制和粘贴代码)

例如:如果您的商品具有 useIt 功能,那么火柴的行为将与打火机不同,因为火柴可以短时间使用一次,而打火机可以多次使用,并且可以长时间使用。更长的时间(直到变得太热并爆炸)。

有关如何创建实例和使用原型(prototype)的更多信息,请参阅 in this answer .

以下是一些如何创建位置和项目的示例代码:

//constructor for Item
var Item=function(args){
//set canuse default to true or whats
// passed in args
this.canUse=(typeof args.canUse==="undefined")?
true:args.canUse;
//cannot create an Item without description
// description doesn't have a default value
if(typeof args.description==="undefined")
throw new Error("Cannot create an item without description");
this.description = args.description;
//default value for usedTimes is 0 but can be an old
// lighter that's been hused hundreds of times
this.usedTimes=(typeof args.usedTimes==="undefined")?
0:args.usedTimes;
};
//behavior on the prototype
Item.prototype.useIt=function(){
this.usedTimes++;
};

//specify a match (should add jsdoc here so you know what args can be passed)
var Match=function(args){
//re use Item code
Item.call(this,args);
//how powerfull is the Match (defaults to 5)
this.burnPower=(typeof args.burnPower==="undefined")?
5:args.burnPower
};
//Match behavior
Match.prototype.useIt=function(args){
if(this.usedTimes!==0){
//tell user it's used up
return;
}
if(args.strikeOn && args.strikeOn.strikableSurface!==true){
//tell user to to define a surfice to strike the match on
return;
}
if(!args.useOn){
//tell user to use it on ...
}
if(args.useOn.toBurn<=this.burnPower){
//burn it and call Item to indicate it's used
Item.prototype.useIt.call(this);
}
};

//constructor for location
var Location = function(args){
this.type=args.type;
if(typeof this.type==="undefined")
throw new Error("Cannot create a location without specifying type");
this.title=(typeof args.title==="undefined")?
args.type:args.title;
//other stuff where type is mandatory
this.items=[];
}
Location.prototype.addItem=function(item){
this.items.push(item);
};
var args={},locations = {};

var loc=new Location({type:"Yard"});
loc.addItem(new Match({description:"A small match"}));
locations[loc.type]=loc;
console.log(locations);

关于javascript - 文字与原型(prototype)对象表示法的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com