zoukankan      html  css  js  c++  java
  • [Javascript] Prototype 2 Object.create()

    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 + "
    ";
        });
        return 'Fence post #'+this.postNum+":
    "+
          'Connected to posts:'+
          list+"
    "+
          '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;
      };  
    };
  • 相关阅读:
    ACR2010_TNF拮抗剂促进SpA患者椎体侵蚀的修复
    2010年10月第1期(ACR2010专刊AS篇)_中信国健临床通讯目录
    ACR2010_中轴型SpA患者使用TNF拮抗剂治疗后放射学进展与全身炎症消退相关
    ACR2010_小剂量依那西普对达到缓解的AS患者有效
    ACR2010_在高风险人群中应用新版RA分类标准可能发现更多临床前RA
    ACR2010_常规医疗环境下TNF拮抗剂对RA骨侵蚀的修复作用
    ACR2010_HLA B27阳性的极早期AS患者停用TNF拮抗剂后疗效持续: 3个月随机安慰剂对照试验后随访40周
    .net委托与事件 中庸
    筛选法因子之和
    POJ2488 A Knight's Journey 解题报告
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3903162.html
Copyright © 2011-2022 走看看