我正在使用jQuery库创建一个插件.
这里我将String.prototype存储在变量中,然后我使用此变量来扩展我的Sting对象.这工作正常.
// String Prototyping store in a variable
// Save bytes in the minified version of js
var StrProto = String.prototype;
String.prototype.toProperCase = function () {
return this.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
// working fine
alert("yogesh kumar".toProperCase());
Run Code Online (Sandbox Code Playgroud)
在下一种情况下,我正在创建存储在abc变量中的m 函数xyz, 这也正常工作.
function xyz(x){
alert(x)
}
var abc = xyz;
// working fine
abc("yogesh kumar");
Run Code Online (Sandbox Code Playgroud)
在最后一种情况下,我将document.createElement存储在变量 标签中,并使用标签创建一个按钮.但这不起作用.
var tag=document.createElement;
$(document.createElement("button")).html("document.Element").appendTo("#myDiv");
// not working
$(tag("button")).html("tag").appendTo("#myDiv");
Run Code Online (Sandbox Code Playgroud)
请检查jsFiddle上的链接:
错误:
在Chrome中 …