Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a method that can affect any value type?

Tags:

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 
like image 792
Travis Avatar asked Feb 18 '15 01:02

Travis


People also ask

Can an object call a method?

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.

What is a method variable?

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.

How do you call a variable from another method in Java?

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).

Which method returns class type of the argument passed as a parameter?

clone() method or define a constructor that takes an object of its class as a parameter.


1 Answers

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:

  1. Your code doesn't mess with other peoples' code.
  2. Other people's code doesn't mess with your code.
  3. You keep the global scope and native prototypes clean.

Several popular JavaScript libraries like underscore and lodash create wrapper constructors for this very purpose.

like image 145
Aadit M Shah Avatar answered Sep 17 '22 18:09

Aadit M Shah