• javascript原型继承


    1.Call对象冒充  继承基类的构造函数的属性,方法

    2.创建一个基类对象作为子类原型的原型  共享基类prototype的方法

    function Polygon(size){
        this.size = size;
    }
    Polygon.prototype.getArea = function(){
        return 0;
    }
    /**矩形*/
    function Rectangle(width,height){
        Polygon.call(this,4);
        this.width = width;
        this.height= height;
        
        if(typeof(Rectangle._initialize=="undefined")){
            Rectangle.prototype.getArea = function(){
                return this.width*this.height/2;
            }
            Rectangle._initialize = true;
        }
    }
    Rectangle.prototype = new Polygon();

    区别:

    为函数动态地增加属性、方法    ----->遍历属性访问
    为函数prototype的属性添加方法----->实例对象可访问,子类可访问

    function Sing(){
    }
    Sing.name = "歌";
    Sing.methed = function(){
        alert('函数名可以当作对象名,进行其他操作');
    }
    Sing.prototype.methed = function(){
        alert('函数名可以当作对象名,进行其他操作');
    }
    for(var item in Sing){
        document.write("属性名:" + item + ";属性值:" + Sing[item] + "<br/>");
    }
    Sing['methed']();
    var obj = new Sing();
    obj['methed']();
    /**
     * var obj = new fun();
     * 等价
     * function fun(){};
     * var obj = {};
     * fun.call(obj)
     
    */

     对象与函数结合:

    obj.fun = fun;   函数作为某一对象的方法值

    fun.call(obj);   将对象作为this,调用函数

  • 相关阅读:
    c++ 两个set合并
    L2-2 小字辈 (25 分)
    L1-1 天梯赛座位分配
    c++ 用 0x3f3f3f3f 设定最大int值的优点
    Treap(树堆)(转)
    new一个二维数组(转)
    Laplacian matrix(转)
    寒假计划制定
    寒假集训日志(八,九,十)——浪浪浪
    寒假集训日志(七)——数据结构
  • 原文地址:https://www.cnblogs.com/xcai/p/2375440.html
Copyright © 2020-2023  润新知