[英]Document.createElement arity
This is related to but not exactly the same as this question. 这与此问题有关但不完全相同。 I recently had some code that broke on several browsers because it wrapped
document.createElement
with a decorator that curried the built-in function. 我最近有一些代码在几个浏览器上破解了,因为它包含了一个带有装饰器的
document.createElement
,该装饰器调整了内置函数。
Opening up the chrome console and typing document.createElement.length
yields 1, in FF/Safari, 2. 打开chrome控制台并输入
document.createElement.length
,在FF / Safari中产生1,2。
Why? 为什么? Is it the optional type extension in mentioned in the other question (which oddly enough is a chrom(ium) thing for webcomponents' custom elements)?
它是另一个问题中提到的可选类型扩展(奇怪的是,对于webcomponents的自定义元素,这是一个chrom(ium)的东西)? What does the standard say about this?
标准对此有何看法?
It is indeed (at least in FF) related to the typeExtension, document.createElement.toString()
in the console returns 确实(至少在FF中)与typeExtension相关,控制台中的
document.createElement.toString()
返回
function createElement(tag, typeExtension) {
if (tag) {
tag = tag.toLowerCase();
}
if (typeExtension) {
typeExtension = typeExtension.toLowerCase();
}
var definition = getRegisteredDefinition(typeExtension || tag);
if (definition) {
if (tag == definition.tag && typeExtension == definition.is) {
return new definition.ctor();
}
if (!typeExtension && !definition.is) {
return new definition.ctor();
}
}
var element;
if (typeExtension) {
element = createElement(tag);
element.setAttribute("is", typeExtension);
return element;
}
element = domCreateElement(tag);
if (tag.indexOf("-") >= 0) {
implementPrototype(element, HTMLElement);
}
return element;
}
##UPDATE 2 github issue - see Jeremy's answer. ##更新2 github问题 - 请参阅Jeremy的回答。
The source code you've quoted is from the Polymer team's Web Components polyfill , not the native browser implementation. 您引用的源代码来自Polymer团队的Web Components polyfill ,而不是本机浏览器实现。 If you try it in a page that doesn't include the polyfill, you'll get different results (Firefox on Ubuntu):
如果您在不包含polyfill的页面中尝试它,您将得到不同的结果(Ubuntu上的Firefox):
> document.createElement.length
1
> document.createElement.toString()
"function createElement() {
[native code]
}"
The reason you get 1
in Chrome is because it currently includes a native implementation of Web Components (which still has .length
of 1), so the polyfill isn't being used. 你的理由
1
在Chrome是因为它目前包括本地实现Web组件(其中仍然有.length
为1),所以不使用填充工具。
The built-in function doesn't yet have .length
of 2 in any browsers. 在任何浏览器中,内置函数的
.length
都不是.length
。 However, the specification of createElement
is going to change soon, so don't rely on this remaining true. 但是,
createElement
的规范很快就会改变,所以不要依赖于这个仍然是真的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.