Warning: creating extensions to native object and/or properties is considered bad form, and is bound to cause problems. Do not use this if it is for code that you are not using solely for you, or if you don't know how to use it properly
I know you can use Object
, String
, Number
, Boolean
, etc. to define a method, something like this:
String.prototype.myFunction = function(){return this;} //only works on strings.
But what I need to be able to do is use that on any value, and access the value in the function.
I googled, and looked here, but couldn't find anything suitable.
2/18/15 Edit: Is there any workaround to having this be a property of any Object if I use Object.prototype
?
Per Request, here is the current function that is used for isString()
function isString(ins) { return typeof ins === "string"; }
Following on a few answers, I have come up with some code and errors caused by it.
Object.prototype.isString = function() { return typeof this === "string"; } "5".isString() //returns false "".isString() //returns false var str = "string" str.isString() //returns false
Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.
A method variable is essentially a variable that has had a function assigned to it, "binding" the function to an instance and enabling you to use the variable to refer to the function - much like you use a runtime function name to refer to a built-in GML function.
You can't. Variables defined inside a method are local to that method. If you want to share variables between methods, then you'll need to specify them as member variables of the class. Alternatively, you can pass them from one method to another as arguments (this isn't always applicable).
clone() method or define a constructor that takes an object of its class as a parameter.
A “dot operator function” is called a method. The cleanest way to create a method in JavaScript that can work on any data type is to create a wrapper. For example:
var Wrapper = defclass({ constructor: function (value) { this.value = value; }, isString: function () { return typeof this.value === "string"; }, describe: function () { if (this.isString()) { alert('"' + this.value + '" is a string.'); } else { alert(this.value + " is not a string."); } } }); var n = new Wrapper(Math.PI); var s = new Wrapper("Hello World!"); n.describe(); // 3.141592653589793 is not a string. s.describe(); // "Hello World!" is a string. function defclass(prototype) { var constructor = prototype.constructor; constructor.prototype = prototype; return constructor; }
By creating your own wrapper constructor you ensure that:
Several popular JavaScript libraries like underscore and lodash create wrapper constructors for this very purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With