开发者社区> 问答> 正文

js策略模式的疑惑

    var performanceS=function(){};
                performanceS.prototype.calculate=function(salary){
                    return salary*4;
                }
                var performanceA=function(){};
                performanceA.prototype.calculate=function(salary){
                    return salary*3;
                }
                var performanceB=function(){};
                performanceB.prototype.calculate=function(salary){
                    return salary*2;
                }
                
                var Bonus=function(){
                    this.salary=null;
                    this.strategy=null;
                };
                Bonus.prototype.setSalary=function(salary){
                    this.salary=salary;
                }
                Bonus.prototype.setStrategy=function(strategy){
                    this.strategy=strategy;
                }
                Bonus.prototype.getBonus=function(){
                    //这里的calculate是另外三个对象原型下的方法这为什么不报错,是怎么实现的
                    return this.strategy.calculate(this.salary);
                }
                
                var bonus=new Bonus();
                bonus.setSalary(10000);
                bonus.setStrategy(new performanceS());
                console.log(bonus.getBonus());//40000
                bonus.setStrategy(new performanceA());

展开
收起
小旋风柴进 2016-05-27 08:14:34 2211 0
1 条回答
写回答
取消 提交回答
  • 简单直白一点儿就是,这就是 js 用类创建对象的方式,可以把 performance 看成是类名,然后 new performace 就是这个类生成的对象。

    var a = new performance,具体细节(简化的)是:

    创建一个对象,`obj
    obj.[[prototype]] = perfomance.prototype; `// 这个属性原本不可以用 js 访问,后来 w3c 把它添加到了标准中,后来使用了新的接口所以又去掉了,但是在 firefox 和 chrome 实现也保留了这个属性叫做 __proto__,你可以在他们的调试窗口看到。
    令this = obj,然后执行 perfomance中定义的代码;
    假设上一步perfomamce的返回值是'ret'。 如果 ret是对象,那么 a = ret, 否则 a = obj;
    然后就是访问 a 的属性(读取,先不谈写入)的时候,给出一个属性,calculate,首先看 a.calculate 是否存在,如果不存在就试图访问 a.[[prototype]].calculate,如果还不存在就访问a.[[prototype]].calculate。。。。

    这就是 js 的原型链,自己百度找其他参考资料(为什么有人一次又一次的问这个问题)

    所以你的代码就是
    a.calculate 就是 a.[[prototype]].calculate 也就是 perfomance.prototype.calculate

    2019-07-17 19:16:51
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载