I am creating a plugin using jQuery library.
Here i am storing String.prototype in a variable then i am using this variable to extend my Sting object. And this is working fine.
// 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());
In the next case i am creating m function xyz which stored in abc variable and this is also working fine.
function xyz(x){
alert(x)
}
var abc = xyz;
// working fine
abc("yogesh kumar");
In the last case i am storing document.createElement in a variable tag and using tag to create a button. but this is not working.
var tag=document.createElement;
$(document.createElement("button")).html("document.Element").appendTo("#myDiv");
// not working
$(tag("button")).html("tag").appendTo("#myDiv");
Please check the link on jsFiddle:
Error:
In Chrome
- Uncaught TypeError: Illegal invocation
in Firefox
- Error: NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument
Why this error?
What is the solution?