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. android OOM 内存溢出

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 一个应用的可用内存是有限的,如果超过了可用的内存,就会内存溢出. 1,避免 已经不用的对 ...

  2. Spring的模块组成

    Spring的模块组成 1.核心容器:核心容器提供 Spring 框架的基本功能(Spring Core).核心容器的主要组件是 BeanFactory,它是工厂模式的实现. BeanFactory ...

  3. 【BFS】【最小生成树】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem G. We Need More Managers!

    题意:给你n个点,点带权,任意两点之间的边权是它们的点权的异或值中“1”的个数,问你该图的最小生成树. 看似是个完全图,实际上有很多边是废的.类似……卡诺图的思想?从读入的点出发BFS,每次只到改变它 ...

  4. TortoiseGit + msysgit 记住帐号密码方法及使用密匙的方法

    Windows 重度用户只能用 for windows 的软件了,所以虽然使用 Git,但还是要找专门的 windows 版本. 最近开始使用 GitHub 来托管一些小项目/兴趣,而自己是重度 wi ...

  5. Windows 0day成功验证之ETERNALBLUE

    本帖由春秋首发~作者:神风 @春秋文阁负责人 方程式又一波0day[该贴有工具]:https://bbs.ichunqiu.com/thread-21736-1-1.html 最近一段时间出现一波高潮 ...

  6. Dijkstra_Liu博客100篇祭

    创建博客,有两年三个月了.今天,写了100篇随笔了,又正值我的15岁生日,还是值得纪念一下. 两年过去了,我从学习:队列.栈.模拟.背包慢慢地变成了:Tarjan.线段树.树剖. 我也从一个初一的天真 ...

  7. redhat 6.6 安装 (LVM)

    http://www.cnblogs.com/kerrycode/p/4341960.html

  8. Spring MVC的异步模式DefferedResult

    原文:http://www.importnew.com/21051.html 什么是异步模式 要知道什么是异步模式,就先要知道什么是同步模式,先看最典型的同步模式: (图1) 浏览器发起请求,Web服 ...

  9. VC6 下 libpng 库的编译与初步使用

      VC6 下 libpng 库的编译与初步使用 目录 libong 库的介绍 VC6 下 libpng 的编译 下载 libpng 与 zlib 进行编译 得到 .lib 文件 初步使用 对 VC6 ...

  10. Selenium2+python自动化56-unittest之断言(assert)

    前言 在测试用例中,执行完测试用例后,最后一步是判断测试结果是pass还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert). 用unittest组件测试用例的时候,断言 ...