js原生设计模式——7原型模式之真正的原型模式——对象复制封装

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>真正的原型模式——对象复制封装</title>
    <script type="text/javascript">
    /***
    *真正的原型模式——对象复制封装
    *基于已有对象浅复制出新对象的模式
    *实参表示已有的模板对象
    */
    //封装原型继承方法
    function prototypeExtend(){
        var _f = function(){};//设置缓存类
        for(var i = 0; i < arguments.length;i++){
            for(var index in arguments[i]){
                _f.prototype[index] = arguments[i][index];
            }
        }
        //返回缓存类的实例
        return new _f();
    }

    var pe = prototypeExtend({
        speed:20,
        swim:function(){
            console.log('游泳速度'+this.speed);
        }
    },{
        run:function(speed){
            console.log('奔跑速度'+speed);
        }
    },{
        jump:function(){
            console.log('做跳跃动作');
        }
    });
    //测试
    pe.swim();
    pe.run(40);
    pe.jump();

    //本例已经通过验证
    </script>
</head>
<body>
    
</body>
</html>

原文地址:https://www.cnblogs.com/koleyang/p/4939559.html