zoukankan      html  css  js  c++  java
  • 面向对象 原型 继承

    相关文章

    继承 圣杯模式

    prototype constructor __prpto__ 三者之间关系

    typeof

    区分数组与对象

    instanceof

    Object.create

    类数组

    好玩的:

    1.字符串函数扩展  ( 一般调用 )
    String.prototype.repeat=function(num){
      return (new Array(num+1)).join(this)
    }

    console.log('good'.repeat(3))

    2. 自定义原型名称  ( 配置调用 )

    Function.prototype.method = function (name, func) {
    if (!this.prototype[name]) {
    this.prototype[name] = func;
    };
    return this;
    }
     
    // 取整
    Number.method("absolute", function () {
    return Math[this < 0 ? "ceil" : "floor"](this);
    });
    console.log((-3.53).absolute());

    // 移除字符串末端空白
    String.method("trim", function () {
    return this.replace(/^s+|s+$/g, '');
    })
    console.log(" meat ".trim());
     
    继承
    function Animal() {
                this.eye = 'two'
                this.foot = 'four'
            }
    
            function Cat(){
                // Animal.call(this)
                Animal.apply(this,arguments)
            }
    
            // Cat.prototype = Animal.prototype   -   only prototype  && diff by  Animal.prototype
    
            // function Cat(){}
            // Cat.prototype = new Animal()
            // Cat.prototype.constructor = Cat
    
            // function Cat(){}
            // function F(){}
            // F.prototype = new Animal()    //  F.prototype = Animal.prototype  
            // Cat.prototype = new F()   
            // Cat.prototype.constructor = Cat
    
            // function Cat(){}
            // var ani = new Animal()
            // for(var i in ani){
            //     Cat.prototype[i] = ani[i]
            // }
            // function deepCopy(p, c) {
            //     var c = c || {};
            //     for (var i in p) {
            //         if (typeof p[i] === 'object') {
            //             c[i] = (p[i].constructor === Array) ? [] : {};
            //             deepCopy(p[i], c[i]);
            //         } else {
            //             c[i] = p[i];
            //         }
            //     }
            //     return c;
            // }
            // deepCopy(new Animal(), Cat.prototype) 
    
    
            console.log(new Animal())
            console.log(new Cat(), new Cat().eye)
    View Code
     
     
  • 相关阅读:
    P3391 文艺平衡树
    隔离村庄(树形dp[01背包])
    cmd指令集
    vs的使用
    博客园第一天
    蓝桥杯 小生物的逃逸 模拟
    蓝桥杯 自行车停放 双向链表
    c++字符数组函数总结
    蓝桥杯 石子游戏 贪心
    蓝桥杯 最大获利 模拟
  • 原文地址:https://www.cnblogs.com/justSmile2/p/10601269.html
Copyright © 2011-2022 走看看