Javascript函数与父函数原型中的原型 [英] Javascript function with prototype within parent function prototype
本文介绍了Javascript函数与父函数原型中的原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
函数foo(){
//做东西
}
foo.prototype = {
// stuff ...
bar:function(){
//用这个做一些事情,这里指的是foo
},
bar.prototype:{
//为bar设置一些定义。
//this去哪里以及它指的是什么?
}
}
解决方案
否。您需要使用
功能栏(){...}
bar.prototype = {.. };
函数foo(){...}
foo.prototype.bar = bar;
虽然这不起作用。没有理由在 foo
s原型上放置 bar
构造函数,因为当使用<$ c实例化条对象时$ c> new((new foo()).bar)(),则不会引用foo实例。您可以同样使用 new foo.prototype.bar()
。
Is this even possible?
function foo() {
// do stuff
}
foo.prototype = {
// stuff...
bar: function() {
// do some things with this, where this refers to foo
},
bar.prototype: {
// set some definitions for bar to work with.
// Where does "this" go and what does it refer to?
}
}
解决方案
No. You'd need to use
function bar() {...}
bar.prototype = {...};
function foo() {...}
foo.prototype.bar = bar;
Although this won't work. There is no reason to put the bar
constructor on foo
s prototype, because when instantiating bar objects by using new ((new foo()).bar)()
, there will be no reference to the foo instance. You could equally use new foo.prototype.bar()
.
这篇关于Javascript函数与父函数原型中的原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文