0

I want to have my own method in JavaScript which can be able to access some property of the preceding method/function Eg:

//default
var arr = [1,6,3,5];
alert(arr.length);

This will alert the length of arr. But I want to use my custom method instead of the length method say len Eg:

//custom
prototype.len = ()=>{
   return prototype.length;
}
var arr = [1,6,3,5];
alert(arr.len);
3
  • 1
    That wouldn't be a method, that would be a property. Commented Mar 22, 2018 at 18:50
  • nope, would be a getter / setter. Have a look at defineProperty Commented Mar 22, 2018 at 18:51
  • Related: stackoverflow.com/questions/14034180/… Commented Mar 22, 2018 at 18:59

4 Answers 4

3

To define a "getter" without using ES6 class syntax you can use Object.defineProperty:

Object.defineProperty(Array.prototype, 'len', {
    get: function() {
        return this.length;
    }
});

Use of .defineProperty will (by default) create a non-enumerable property which ensures that your new property doesn't appear inadvertently in the results of a for .. in ... loop.

The question of whether it's appropriate to add such a property to a built-in class is a matter of some debate. Some languages explicitly encourage it, some don't.

Sign up to request clarification or add additional context in comments.

3 Comments

@leaf that's the wrong way to do it - you've defined a method which must be invoked with function call syntax, not a getter that automagically invokes the specified function when the property is accessed.
I know I'm wrong, and I know I defined a method rather than a getter :-D I've updated my answer to avoid mislead the reader.
Thank you @Alnitak! ...your answer was what I'm looking for. The others are just busy asking me to use a function rather than a property/method like you did. Big thanks to everyone who spent h(is/er) time to help me
3

You may define your own method for prototype:

// also you may use Object.defineProperty
Array.prototype.len = function() {
   return this.length // --> this will be an array
}

and then call the function

[1, 2, 3].len()

BUT

It is a bad practice to define any new function in built-in prototypes - you can accidentally redefine existing method or method with the same name will be added in the future and it will cause unpredictable behavior.

Better approach either create your own prototype and use it

function MyArray () {
// your code
}

MyArray.prototype = Object.create(Array.prototype) // inheritance 

MyArray.prototype.len = function () {
    return this.length
}

or just create simple function and pass an array as an argument to it or as this:

as argument:

function len (array) {
    return array.len
}

as this:

function len() {
    return this.len
}

// invoking function
len.call(array)

4 Comments

The OP appears to want a getter, not a method.
@Alnitak well, may be. In the question i saw method. But it is still bad idea to add anything to built-in prototype :-)
@Alnitak Maybe you overinterpreted a little indeed, and in my opinion, customizing a builtin prototype, even with property affectation, is not always evil, it depends on the use case.
@leaf see OP's comment on my answer - he did specifically want a getter, not a method. And FWIW, I frequently put new methods in the built-in classes.
0

You can do it like this :

Array.prototype.len = function () {
  return this.length;
};

But there can be a better option, see comments below.

2 Comments

You can, but you shouldn't. Use Object.defineProperty(Array.prototype, 'len', { ... }) instead.
@Alnitak Alright, well spotted. At reader, in the following article, the "Description" section explains why you should probably not follow my suggestion (although it's not bad in itself, it depends on the use case) : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/….
-1

You can create your custom methods or properties to built-in objects, in the example you gave, you want to alter the Array built-in object and you have to be specific. For instance:

Array.prototype.len = function() { return this.length; };

[1, 2, 3].len(); // this will return 3

Keep in mind that this refers to the array you're calling the method and if you use an ES6 arrow function, the scope will be affected and therefore the this value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.