zoukankan      html  css  js  c++  java
  • [Javascript] Prototype 1

    You can add prototype to any object in Jacascript likes Object, Array, String...

    prototype 有继承的作用,比如说我有一个String的对象,我可以访问Object的prototype hasPrototype() function,

    StrObj -- String --Object,

    你每创建的一个StrObj, 它之所以可以使用.shift(), pop(), push() methods是应为那些methods都是String object继承下去的。

    Array.prototype.countCattle = function ( kind ){
      var numKind = 0;
      for(var i = 0; i<this.length; i++){
        if(this[i].type == kind){
          numKind++;
        }
      }
      return numKind;
    };
    alert(canyonCows.countCattle("calf")+valleyCows.countCattle("bull")+forestCows.countCattle("cow"));
    Object.prototype.noCalvesYet = function () {
      if(this.type == "cow" && this.hadCalf == null){
        return true;
      }
      return false;
    };
    Array.prototype.countForBreeding = function(){
      var numToBreed = 0;
      for(var i = 0; i < this.length; i++){
        if(this[i].noCalvesYet()){
          numToBreed++;
        }
      }
      return numToBreed;
    };
  • 相关阅读:
    如何绕过chrome的弹窗拦截机制
    自我介绍
    注册页面的编写
    Roadmap学习目标
    Position
    poj2506 Tiling
    poj3278 Catch That Cow
    poj3624 Charm Bracelet
    钢条切割问题带你彻底理解动态规划
    poj1328 Radar Installation
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3902310.html
Copyright © 2011-2022 走看看