我想澄清一下,我知道他们每个人的职责。我只是无法理解这些元素的组织。所以我知道在像String.concat
这样的东西中,concat是String
的原型(prototype)。但是,Window.scrollY
会是什么?我想说scrollY是Window的原型(prototype),但我有一种直觉,事实并非如此。另外,当我进入像 this 这样的页面时,有一个很长的 API 列表。窗口是一个API吗?那么,String 也是一个 API 吗?
最佳答案
So I know that in things like String.concat, concat is a prototype of String.
你看起来有点困惑。虽然有 String.prototype.concat
,但没有 String.concat
,它是由 String 的实例继承的。它将被称为 Property of the String Prototype Object或 inherited property of a string instance .
However, what would Window.scrollY be?
我怀疑你的意思是window.scrollY
,注意小写的w。它是Window object的属性(property)。 。由于它对于不同的窗口可能会有所不同,因此它是对象本身的属性,是“自己的属性”,而不是继承的。
窗口对象是宿主对象,因此必须符合 ECMAScript 的某些部分,但通常可以自由地做任何他们想做的事情,只要它们不与适用于它们的标准部分相矛盾。例如,Host 对象不必实现任何类型的继承,并且可以为 typeof 返回奇怪的值。 :
Object (host and does not implement [[Call]]) Implementation-defined except may not be "undefined", "boolean", "number", or "string".
Global object不继承任何东西,它只是"is"。没有 Global 构造函数,您无法使用 new Global()
实例化新的 global 对象,并且没有 Global.原型(prototype)
。
但是,您可以使用 window.open()
创建一个新的 Window 对象,因此有一个 Window 构造函数,并且大多数浏览器确实实现了原型(prototype)继承。由于 window 是全局对象的同义词,因此它变得非常困惑。例如,在某些浏览器中:
Object.getPrototypeOf(this) === Object.getPrototypeOf(window) // true
这导致了这样的想法:有一个 Global.protoype
,它继承自内置的 Object 构造函数:
Object.getPrototypeOf(Object.getPrototypeOf(this)) == Object.prototype // true
还有:
// Note that in global code, this references the global object
alert(typeof this.foo); // undefined
Window.prototype.foo = function(){alert('I am foo')};
alert(typeof this.foo); // function
this.foo(); // I am foo
var w = window.open(); // Opens a new window
w.foo(); // TypeError: 'undefined' is not a function (evaluating 'w.foo()')
但是,这可能是某些浏览器特有的,不应依赖它来实现任何类型的继承。
关于javascript - 我很困惑。什么是 window.scrollY,它与 String.concat 之类的东西有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234525/