function Fencepost (x, y, postNum){
this.x = x;
this.y = y;
this.postNum = postNum;
this.connectionsTo = [];
}
Fencepost.prototype = {
sendRopeTo: function ( connectedPost ){
this.connectionsTo.push(connectedPost);
},
removeRope: function ( removeTo ){
var temp = [];
for(var i = 0; i<this.connectionsTo.length; i++){
if(this.connectionsTo[i].postNum != removeTo){
temp.push(this.connectionsTo[i]);
}
}
this.connectionsTo = temp;
},
movePost: function (x, y){
this.x = x;
this.y = y;
},
valueOf: function (){
return Math.sqrt( this.x*this.x + this.y*this.y );
},
toString: function(){
var list = this.connectionsTo.map(function(each){
return each.postNum + "\n";
});
return 'Fence post #'+this.postNum+":\n"+
'Connected to posts:'+
list+"\n"+
'Distance from ranch: '+this.valueOf()+
' yards'; }
};
var genericPost =
{x: 0, y: 0, postNum: undefined,
connectionsTo: undefined,
sendRopeTo: function ( connectedPost ) {
if(this.connectionsTo == undefined){
var postArray = [ ];
postArray.push(connectedPost);
this.connectionsTo = postArray;
} else {
this.connectionsTo.push(connectedPost);
}
}
};
var post1 = Object.create(genericPost); //将继承genericPost中所有的属性
var post2 = Object.create(genericPost);
post1.x = -2;
post1.y = 4;
post1.postNum = 1;
post2.x = 5;
post2.y = 1;
post2.postNum = 2;
post1.sendRopeTo(post2);
post2.sendRopeTo(post1);

Below are the data for three posts that need to be created using the genericPost as a prototype (as in the last challenge). The cowboys have given you all the data you’ll need to build each using the prototype and then assign unique property values through modification. Call each of your posts post<number>, just like you did in the last challenge.

  1. x: 0, y: -3,
    postNum: 8,
    connectionsTo: 10

  2. x: 6, y: 8,
    postNum: 9,
    connectionsTo: 10

  3. x: -2, y: 3,
    postNum: 10,
    connectionsTo: 8, 9

The cowboy-devs have prepared a list of additions that need to happen for certain special fence posts. After you’ve built the above three posts, add properties to those post where the cowboy-devs have deemed appropriate.

  1. Any fence posts with an even ‘y’ coordinate have a birdhouse, and therefore have a numBirds property initially set to 0.
  2. Any fence posts connected to Post #9, but are not Post #9, have a property of weathervane initially set to “N”.
  3. Even numbered fence posts have emergency lights, and a lightsOn property initially set to false.

The base fencepost is again provided for your reference. There are many single lines of code to be entered on this one, so be careful to assign all properties necessary.

var genericPost = {
x: 0,
y: 0,
postNum: undefined,
connectionsTo: undefined,
sendRopeTo: function ( connectedPost ) {
if(this.connectionsTo == undefined){
var postArray = [ ];
postArray.push(connectedPost);
this.connectionsTo = postArray;
} else {
this.connectionsTo.push(connectedPost);
}
}
};
var post8 = Object.create(genericPost);
var post9 = Object.create(genericPost);
var post10 = Object.create(genericPost);
post8.x = 0;
post8.y = -3;
post8.postNum = 8;
post8.sendRopeTo(post10);
post9.x = 6;
post9.y = 8;
post9.postNum = 9;
post9.sendRopeTo(post10);
post10.x = -2;
post10.y = 3;
post10.postNum = 10;
post10.sendRopeTo(post8);
post10.sendRopeTo(post9);
post9.numBirds = 0;
post10.weathervane = "N";
post8.lightsOn = false;
post10.lightsOn = false;

So now that’s there’s eleventy-billion fence posts everywhere, the cowboy-devs have noticed a significant drain on their memory resources. They’d like you to take a look around the Fencepost constructor and see if there’s anything you can add to a prototype, so that every stinkin’ fence post doesn’t have to carry around anything that it could get from just one place.

Below is the current status of the constructor, with some additions the cowboy-devs have made to improve functionality of the fence post objects. Your job is to identify the portions of the constructor that should be available to ALL fenceposts, and put those in a prototype for fence posts. Your answer should include the modified constructor as well as the newly designed prototype for that constructor. Good luck, pardner.

function Fencepost (x, y, postNum){
this.x = x;
this.y = y;
this.postNum = postNum;
this.connectionsTo = [];
this.sendRopeTo = function ( connectedPost ){
this.connectionsTo.push(connectedPost);
};
this.removeRope = function ( removeTo ){
var temp = [];
for(var i = 0; i<this.connectionsTo.length; i++){
if(this.connectionsTo[i].postNum != removeTo){
temp.push(this.connectionsTo[i]);
}
}
this.connectionsTo = temp;
}
this.movePost = function (x, y){
this.x = x;
this.y = y;
};
}

Answer:

function Fencepost (x, y, postNum){
this.x = x;
this.y = y;
this.postNum = postNum;
this.connectionsTo = [];
} Fencepost.prototype = {
sendRopeTo: function ( connectedPost ){
this.connectionsTo.push(connectedPost);
},
removeRope: function ( removeTo ){
var temp = [];
for(var i = 0; i<this.connectionsTo.length; i++){
if(this.connectionsTo[i].postNum != removeTo){
temp.push(this.connectionsTo[i]);
}
}
this.connectionsTo = temp;
},
movePost : function (x, y){
this.x = x;
this.y = y;
};
};

[Javascript] Prototype 2 Object.create()的更多相关文章

  1. Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create

    Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...

  2. javascript的创建对象object.create()和属性检测hasOwnPrototype()和propertyIsEnumerable()

    Object.create("参数1[,参数2]")是E5中提出的一种新的对象的创建方式. 第一个参数是要继承到新对象原型上的对象; 第二个参数是对象属性.这个参数可选,默认为fa ...

  3. Object.create

    var emptyObject = Object.create(null); var emptyObject = Object.create(null); var emptyObject = {}; ...

  4. 前端开发者进阶之ECMAScript新特性【一】--Object.create

    Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数:prototype 必需.  要用作原型的对象. 可以为 nul ...

  5. firefox-Developer开发者站点——关于Object.create()新方法的介绍

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create Objec ...

  6. js-new、object.create、bind的模拟实现【转载备忘】

    //创建Person构造函数,参数为name,age function Person(name,age){ this.name = name; this.age = age; } function _ ...

  7. js Object.create 初探

    1.作用 Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. https://developer.mozilla.org/zh-CN/docs/W ...

  8. ECMAScript新特性【一】--Object.create

    Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数: prototype 必需.  要用作原型的对象. 可以为 nu ...

  9. ES5 Object.create 方法

    Object.create(proto[, propertiesObject])The Object.create() method creates a new object with the spe ...

随机推荐

  1. 【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM

    题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...

  2. Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�

    今天碰到一个很讨厌的问题,使用nodejs 接收Azure service bus队列消息的时候,出现了:@strin3http//schemas.microsoft.com/2003/10/Seri ...

  3. [POI2015]Pieczęć

    [POI2015]Pieczęć 题目大意: 一张\(n\times m(n,m\le1000)\)的方格纸,有些格子需要印成黑色,剩下的格子需要保留白色. 你有一个\(a\times b(a,b\l ...

  4. tsinsen A1333

    可以用二维树状数组套值域线段树来做,复杂度:O( (n*n+q) * logn logn log10^9 ) 但作为作为整体二分的例题,还是用整体二分来写了一下.对整体二分有一点感觉了. 整体二分,顾 ...

  5. BZOJ 4521 CQOI 2016 手机号码 数位DP

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 539  Solved: 325[Submit][Status ...

  6. Sql server 存储过程基础语法

    一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 declare @user1 nvarchar() select @user1='张三' pr ...

  7. CentOS6.X关闭防火墙

    一.关闭防火墙 1.重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off 2.即时生效,重启后失效: 开启:service ipta ...

  8. 读书笔记_Effective_C++_条款三十一:将文件间的编译依存关系降至最低(第三部分)

    下面来谈谈书中的第二部分,用Interface Classes来降低编译的依赖.从上面也可以看出,避免重编的诀窍就是保持头文件(接口)不变化,而保持接口不变化的诀窍就是不在里面声明编译器需要知道大小的 ...

  9. SqlServer收缩日志文件

    通过收缩的方法可以释放所占空间. 第一步:将数据库的模式调整为简单模式 右击数据库名->'属性'->'选项'->恢复模式改成'简单'->点'确定'按钮. 第二步:收缩文件 右键 ...

  10. CLR基础,CLR运行过程,使用dos命令创建、编译、运行C#文件,查看IL代码

    CLR是Common Language Runtime的缩写,是.NET程序集或可执行程序运行的一个虚拟环境.CLR用于管理托管代码,但是它本身是由非托管代码编写的,并不是一个包含了托管代码的程序集, ...