0

I found the following way of calling prototype methods in javascript :

this.abc(x,y,z).cnt;

What will this statement call, the arguments and what will be the return values. I saw this kind of usage in my application that Im currently working on.

2 Answers 2

2

this.abc(x,y,z).cnt means

  • .abc is a method of your current object (or whatever this is defined to be)
  • .abc accepts x, y, z and returns an object
  • The returned object has a property called cnt
  • this.abc(x,y,z).cnt basically gets the value of that property

If you are familiar with languages like Python, it is equivalent to

self.abc(x, y, z)["cnt"]
Sign up to request clarification or add additional context in comments.

1 Comment

ya fine thanks. As I have not used that pattern, I got confused whether will there any arg manipulation while passing values to the prototype method
0

That this call method abc with parameter x,y,z and then take cnt property.

Look at this example, and focus to variable data in abc method.

// init a class
function Obj(){}

// add method abc
Obj.prototype.abc = function(x, y, z) {
    var cnt = x + y + z;
    var data = {cnt: cnt};

    // return object with cnt property
    return data;
}

const obj = new Obj();
const x = 1, y = 2, z = 3;

// cnt = 6
var cnt = obj.abc(x,y,z).cnt;

// equal with this
var data = obj.abc(x, y, z);
console.log(data.cnt)

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.