首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文字与原型对象表示法的数据结构

文字与原型对象表示法的数据结构
EN

Stack Overflow用户
提问于 2013-12-19 08:32:41
回答 1查看 183关注 0票数 1

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

代码语言:javascript
复制
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..
}

并且可以像这样定位信息(例如):

代码语言:javascript
复制
currentLocation = locations[currentLocation]["exits"][direction]

..。但当我从原型中定义对象时,我不太确定如何排列和定位对象:

代码语言:javascript
复制
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;

在这里组织和引用我的数据的最佳方式是什么?(在这种情况下,使用文字或原型方法有什么特别的好处吗?)

EN

回答 1

Stack Overflow用户

发布于 2013-12-19 09:05:40

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

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

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

下面是一些如何创建位置和物品的示例代码:

代码语言:javascript
复制
//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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20671385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档