Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问函数继承模式中的高级方法(一个反例出人意料地起作用!)_Javascript_Inheritance_Functional Programming - Fatal编程技术网

Javascript 访问函数继承模式中的高级方法(一个反例出人意料地起作用!)

Javascript 访问函数继承模式中的高级方法(一个反例出人意料地起作用!),javascript,inheritance,functional-programming,Javascript,Inheritance,Functional Programming,在理解Crockford引入的函数继承模式的过程中,我带来了代码的和平: var person = function(spec) { var that= {}; that.name = spec.name; that.job = spec.job || ''; that.greeting = function() { console.log("Hello, I'm " + this.name + " and I'm a " + this.job);

在理解Crockford引入的函数继承模式的过程中,我带来了代码的和平:

var person = function(spec) {
    var that= {};
    that.name = spec.name;
    that.job = spec.job || '';
    that.greeting = function() {
        console.log("Hello, I'm " + this.name + " and I'm a " + this.job);
    };
    return that;
};

var coder = function(spec) {
    var that = person(spec);
    that.job = "coder";
    // that.superior_greeting= that.superior('greeting');
    that.superior_greeting = that.greeting;
    that.greeting = function(){
        console.log("I write code");
    };
    return that;
};
让我们创建
coder
实例
aCoder=coder({name:“tarsalah”})
,调用
问候语
方法,打印出我所怀疑的
我写的代码

我的问题是:为什么运行
高级问候语
会给我一个不同的结果(打印
你好,我是tarrsalah,我是编码员

superior\u问候语
是否指向
。问候语
,是否修改
问候语
方法也应修改
superior\u问候语

ps:

之所以如此,是因为

that.superior_greeting = that.greeting;
先于

that.greeting = function(){
    console.log("I write code");
};
编码者
覆盖之前,将.superior\u问候语
设置为该问候语的旧版本

就像

that.superior_greeting = function() {
    console.log("Hello, I'm " + this.name + " and I'm a " + this.job);
};
that.greeting = function(){
    console.log("I write code");
};
如果你把原来的两条线颠倒过来,你会得到不同的结果

“是否
superior\u问候语
指向
。问候语
,是否修改问候语方法也应修改
superior\u问候语
?”

不可以。
superior\u问候语
并不像你想象的那样指向
问候语
。JavaScript没有指针,尽管它的对象(包括函数)作为引用保存

所以在你这样做的时候:

that.superior_greeting = that.greeting;
that.greeting = function(){
    console.log("I write code");
};
superior\u greeting
包含对与
greeting
相同功能的引用。”。但是,它没有指向
问候语
属性本身的指针


所以基本上你没有复制这个函数,但是你复制了同一个函数的引用,它存在于内存中的某个地方

由于您没有指向
.greeting
属性的指针,因此在
.superior\u greeting
中无法看到对该属性的更改

所以当你这样做的时候:

that.superior_greeting = that.greeting;
that.greeting = function(){
    console.log("I write code");
};
…您创建了一个新函数,并将该新函数的新引用分配给
.greeting
属性


现在,
.greeting
.superior\u greeting
正在引用两个不同的函数。

因此,将函数的代码分配给另一个副本,而不仅仅是函数的引用?是的。如果
greeting
superior\u greeting
是函数、数字、对象等,结果将是相似的。因此Crock ford引入了一个新函数(
Object.superior
),以获得相同的结果(代码示例请参见我前面的问题)@tarsalah:不,将一个函数分配给另一个函数不会复制该函数的代码。它复制对函数的引用。需要明确的是,对象永远不会被复制。只复制对对象的引用。很好!,你能解释一下Object.superior方法在他书中的作用吗?@tarrsalah:IMO,它的作用是被放在一个壁橱里遗忘。。。但是为了回答你另一个问题的最后一句话,如果你刚刚将
that.get\u name
分配给
super\u get\u name
,当你调用它时,
get\u name
中的
this
的值将不会是
that
对象,因为你已经从
that
中分离了
get\u name
superior
所做的是返回一个函数,该函数从
that
对象调用原始方法,因此方法中的
this
将作为对
that
对象的引用感谢您的澄清。