我见过几个 JavaScript 库在文件开头有这种语法。这条语句是什么意思,运行这条语句后getClass的值是什么,为什么需要这个?另外,开头的分号有什么用?
;(function (window) {
// Convenience aliases.
var getClass = {}.toString, isProperty, forEach, undef;
// remaining function goes here
}
最佳答案
在 ECMAScript 5 中,对象有一个内部 [[Class]] 属性,根据 8.6.2只能通过Object.prototype.toString
访问:
The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through
Object.prototype.toString
(see 15.2.4.2).
{}.toString
是 Object.prototype.toString
的快捷方式。
例如,getClass
可用于测试对象是否为 Arguments object :
getClass.call(obj) === "[object Arguments]"
关于javascript - 在 JavaScript 中创建别名的目的是什么?这个 JavaScript 语句有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992729/