JavaScript函数封装在对象之外
请参阅以下脚本:
var x = function(param){
this.data=param;
this.y = function(){
alert(this.data)
}
return this;
}
/*
x.prototype.z = function(){
alert(this.data);
}
*/
x(123).y();
x(123).z(); // This should behave same as y()
当我调用x(123).y()时,消息显示123。在 x() 内部声明的函数 y()
现在我想声明另一个函数 z() 位于 x() 之外,但行为与 y() [与x()关联]
可能吗?如果可以的话怎么办?
Please see the following script:
var x = function(param){
this.data=param;
this.y = function(){
alert(this.data)
}
return this;
}
/*
x.prototype.z = function(){
alert(this.data);
}
*/
x(123).y();
x(123).z(); // This should behave same as y()
When I call x(123).y() then message displays 123. The function y() declared inside x()
Now I want to declare another function z() which will reside outside x() but will behave same as y() [associate with x()]
Is it possible? If possible how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用
x()
时缺少new
。否则,函数体内的this
将引用全局对象(浏览器上下文中的window
),而不是x
的实例。这就是为什么调用
z()
(取消注释代码后)不起作用的原因。调用y()
仅在您创建全局变量data
时偶然发生,其值将被下一次调用x()
覆盖。我不知道你想实现什么目标,从我看来,这很可能不是一个好主意。无论如何,下面是一个如何在创建对象时摆脱显式
new
的示例:You're missing a
new
when callingx()
. Otherwise,this
within the function body will refer to the global object (window
in browser contexts) and not an instance ofx
.That's the reason why calling
z()
(after un-commenting the code) doesn't work. Callingy()
only works by coincidence as you create a global variabledata
, whose value will be overwritten by the next call tox()
.I have no idea what you're trying to accomplish, and from what I can see, it most likely isn't a good idea. Anyway, here's an example of how to get rid of explicit
new
when creating objects:这是可能的,如果您修复了创建
x
的方式,则注释掉的z
函数应该按原样工作。请注意,如果您的函数 y 只处理实例属性(在您的代码中就是如此),则不必在构造函数内声明,并且这样做会产生显着的内存成本(每个实例都会获得该函数的自己的副本)。考虑到所涉及的成本,只有在出于主要数据隐藏原因而绝对必须这样做时,您才愿意这样做。
编辑:抱歉,我错过了一些东西:您缺少
new
关键字,您的示例应该是:...并且您的构造函数不应返回
this
。完整示例:
这是我上面提到的 Crockford 文章,但同样,它对内存有很大的影响。
It is possible, and your commented-out
z
function should work as is if you fix how you're creatingx
s.Note that your function
y
doesn't have to be declared inside the constructor if it's only dealing with instance properties (which in your code it is), and doing so has a significant memory cost (every instance gets its own copy of that function). You only want to do that if you absolutely have to for major data hiding reasons, given the costs involved.Edit: Sorry, I missed something: You're missing the
new
keyword, your examples should be:...and your constructor shouldn't return
this
.Complete example:
This is the Crockford article I mention above, but again, it has big memory implications.
不确定这是否是您想要的,但这是一个名为
generator
的构造函数,它返回一个对象,并且在其外部使用new
关键字定义了一个原型方法。正如其他人所说:
this
,new
关键字调用它,this
声明的方法都是公共的除非你使用var
在这种情况下它们变成私有的Not sure if this is what you want, but this is a constructor named
generator
which returns an object, and a prototypal method is defined outside of it with thenew
keyword.As others stated:
this
in the constructornew
keywordthis
are public unless you usevar
in which case they become private