如何阅读 JavaScript 中的点表示法?
举个例子,这个语句
window.Number.constructor.prototype.constructor();
读起来像路径吗?
C:\Users\Vista\Documents\Work\text.txt
从左到右,
window:\Number\constructor\prototype\constructor()
window
是根对象,Number
是 window
内的对象,constructor
是一个对象在 Number
中,prototype
是 constructor
中的一个对象,而 constructor()
是 prototype< 中的一个对象/代码>?
就像在这个陈述中一样
window.document.myForm.textBox.value;
,这等于
[object].[object].[object].[object].1
物体实际上没有相互作用?
或
实际值是从右向左读取的,其中每个对象都直接作用于其左侧的对象吗?
其中
window.Number.constructor.prototype.constructor();
equals
[object] . function Number() { [native code] } . function Function() { [native code] } . function prototype() { [native code] } . function anonymous() { }
as
window.Number(9.256).toFixed(2);
equals
[object].(9.256).(9.26);
其中 toFixed
是使用 Number
对象的返回值的属性,并且结果存储为 window
的属性目的?
正如你可能知道的那样,我在这里有点纠结:)只是很难理解点的概念。 我确信 Java 背景会有所帮助,但不幸的是,我(还)没有。
For the sake of an example, is this statement
window.Number.constructor.prototype.constructor();
read like a path?
C:\Users\Vista\Documents\Work\text.txt
From left to right
window:\Number\constructor\prototype\constructor()
where window
is the root object, Number
is an object inside window
, constructor
is an object inside Number
, prototype
is an object inside constructor
and constructor()
is an object inside prototype
?
Just like in this statement
window.document.myForm.textBox.value;
which equals
[object].[object].[object].[object].1
where the objects aren't actually acting on each other?
OR
Are the actual values read from right to left, where each object is acting on the object directly to the left of it?
Where
window.Number.constructor.prototype.constructor();
equals
[object] . function Number() { [native code] } . function Function() { [native code] } . function prototype() { [native code] } . function anonymous() { }
as
window.Number(9.256).toFixed(2);
equals
[object].(9.256).(9.26);
where toFixed
is a property that's using the return value of the Number
object and the result is stored as a property of the window
object?
As you can probably tell, I'm kinda tangled up over here :) Just having difficulty wrapping my head around the dot concept. I'm sure a background in Java would help, but unfortunately, I don't have one (yet).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从左到右阅读。 每个“事物”都解析为一个对象。 对象可以具有属性或功能。 属性是另一个对象,而对象又可以拥有自己的属性和功能。 如果它是一个函数,那么为了成为合法的语法,该函数必须返回一个对象。 那么右侧的链接项必须是该对象的属性(或函数)。
像 jQuery 这样的框架的工作原理是让每个方法返回自身的副本,以便方法可以链接在一起。
在您的第一个示例中,它指的是一系列对象属性,除了最后一个是函数。 在第二个例子中,它调用 window 对象上的一个函数,该函数返回一个具有 toFixed() 函数的 Number 对象。
Read from left to right. Each "thing" resolves to an object. Objects can have properties or functions. A property is another object, which can in turn have its own properties and functions. If it is a function, then to be legal syntax the function must return an object. Then the chained item to the right must be a property (or function) of that object.
A framework like jQuery works by having each of it's methods return a copy of itself so that methods can be chained together.
In your first example it is referring to a chain of object properties, except the last one which is a function. In the second, it invokes a function on the window object, which returns a Number object that has a toFixed() function.
是的,或者就像 C 中
struct
中的字段一样。它实际上是一堆哈希表或字典。 所以你的例子是“窗口对象,一个名为“Number”的项目,其中包含一个名为“constructor”的项目,其中包含一个名为“prototype”的项目——这是命名方法的地方——其中包含一个方法命名构造函数。” 最后的
()
意思是“并将其视为不带参数的函数”。Yes, or like a field in a
struct
in C. It's actually a bunch of hash tables or dictionaries. So your exampleIs "The window object, an item named 'Number', which contains an item named 'constructor', which contains an item named 'prototype' --- which is where the methods are named --- which contains a method named constructor." That final
()
means "and treat this as a function with no arguments."左到右。
Left to right.