JavaScript 中类的嵌套和 this 值
我可以用 Javascript 编写嵌套类吗?
function A()
{
this.a;
this.B = function()
{
this.ab ;
this.C = function()
{
this.ab = 0;
}
}
}
如果上面的代码正确的话
1.How do I declare an object of type B 2.Whose property is ab.A() 's or B() 's?. 3.Inside B() where does the 'this' points to.To A() Or to B()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的示例中,“类”将是特定于实例的。你确定你想要那个吗?您可能正在寻找更多类似的内容:
尽管“嵌套”类无法访问“私有成员”,因为 JavaScript 一开始就没有这些成员。
至于您的示例:
new A()
,并访问B
,例如new new A().B()
1,或用变量替换new A()
。B
实例的属性。B
的实例(除非使用Function.call
或Function.apply
)。1 是的,它有效!
In your example, the "classes" will be instance-specific. Are you sure you want that? You might be looking for something more along the lines of:
Although the "nested" classes won't be able to access "private members" because JavaScript doesn't have those in the first place.
As for your example:
new A()
, and accessB
, saynew new A().B()
1, or replacingnew A()
with a variable.B
instance.B
(unlessFunction.call
orFunction.apply
is used).1 Yes, it works!
摘要:
不使用 new 运算符调用函数意味着 this 将引用创建该函数的对象。
对于全局变量,该对象是窗口对象。
使用
new
调用 - 函数的行为类似于类中的构造函数,this
引用将创建的此实例1.如何声明 B 类型的对象< /strong>
第一种方法(出于好奇) - 通过调用
A
而不使用new
运算符this
将引用window< /code>object 和
B
方法以及所有其他声明的内容this
泄漏到全局范围,因为A == window.A
=>true
或来自 A 的实例:
小心。
2.谁的属性是 ab.A() 或 B() ?
如上所述,这取决于我们如何访问它:
请查看此内容,
但
3.在 B() 内部,'this' 指向哪里。To A() 还是 B()?
如上:)
Abstract:
Calling to function without using
new
operator means thatthis
will be refer to object in which that function was created.For global variables this object is window object.
Calling using
new
- function behave as constructor like in classes andthis
refers to this instance which will been created1.How do I declare an object of type B
First way( as a curiosity ) - by calling to
A
without usingnew
operatorthis
will be refer towindow
object andB
method and all other what was declared withthis
leaks to global scope becauseA == window.A
=>true
or from instance of A:
Be careful.
2.Whose property is ab.A() 's or B() 's?.
As above, it depends of how we'll accessing to it:
Check this out
but
3.Inside B() where does the 'this' points to.To A() Or to B()?
As above:)
这是非常不正统的,但是没有什么可以阻止你在 JavaScript 中嵌套构造函数。
从您的示例中,您可以从 A 的实例访问 B 函数:
回答您的其他问题:
B 内部的
this
指的是什么取决于B
如何 >调用。如果您将B
作为构造函数调用,并使用new
关键字,this
将是一个继承自 B 原型的新对象。如果您在没有
new
的情况下调用 B,它将被视为方法,并且this
将是调用该方法的 A 的实例。C 也是如此。如果用
new
调用 C,C 内部的this
将是一个继承自 C 原型的新对象。或者C可以是B的方法,这更有意义。您想要的是这样的东西吗:DEMO
最后,请注意,虽然像这样的嵌套构造函数并不太常见,没有什么可以阻止您添加到
B
的原型,就像添加任何其他构造函数一样更新了演示
It's very unorthodox, but there's nothing stopping you from nesting constructor functions in JavaScript.
From your example, you can access the B function from an instance of A:
To answer your other question:
What
this
refers to inside of B depends on howB
is invoked. If you callB
as a constructor, with thenew
keyword,this
will be a new object inheriting from B's prototype.If you call B without
new
, it will be treated as a method, andthis
will be the instance of A on which the method was called.And so on with C. If C is called with
new
,this
inside of C will be a new object inheriting from C's prototype. Or C can be a method of B, which makes a lot more sense. Is something like this what you're wanting:DEMO
Finally, note that, though nesting constructors like this isn't too common, there's nothing stopping you from adding to
B
's prototype just like you would any other constructorUpdated Demo