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;
    };
  • 相关阅读:
    react 滑动删除组件
    004-Java进制转换
    003-JavaString数据类型
    002-Java数据类型
    001-Java命名规范
    【leetcode】804
    【MySQL】基本语句
    【python】
    hiveSql常见错误记录
    【数据库】-基本特性
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3902310.html
Copyright © 2011-2022 走看看