5

I am reading the book "Javascript: The good parts".
Now I am reading chapter about Augmenting Types:

Function.prototype.method = function (name, func) {
   this.prototype[name] = func;
   return this;
};

UPDATE:
Why following code does not work?

js> Function.prototype.method("test", function(){print("TEST")});
typein:2: TypeError: this.prototype is undefined

But following code works without problems:

js> Function.method("test", function(){print("TEST")});
function Function() {[native code]}

Why this code works?

js> var obj = {"status" : "ST"};
js> typeof obj;
"object"
js> obj.method = function(){print(this.status)};
(function () {print(this.status);})
js> obj.method();
ST

"obj" is object.
But I can call method "method" on it.
What is the difference between Function.prototype.method and obj.method?

3 Answers 3

5

this refers to Function.prototype because you called .method on that. So, you're using Function.prototype.prototype which does not exist.

Either use Function.method(...) or this[name] = ... to eliminate one of the .prototypes.

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

1 Comment

Thank you. Now I understand. I did not pay attention on following line this.prototype[name] = func;
5

Because:

Function instanceof Function           // <--- is true
Function.prototype instanceof Function // <-- is false
  • Function.prototype is an Object, and does not inherit anything from the Function contructor.
  • Function is a constructor, but also a function, so it inherits methods from Function.prototype.

  • When calling Function.method, you're calling the method method of an instance of Function. So, this points to the created instance of Function.
  • When calling Function.prototype.method, you're invoking an ordinary method of an object. this points to Function.prototype.

To clarify, here's an example:

Function.method()                // is equivalent to
(function Function(){}).method()
(new Function).method()          // Because Function is also a function

Function.prototype.method // No instance, just a plain function call

3 Comments

"When calling Function.prototype.method, you're invoking an ordinary function. this points to undefined (strict mode) or window." - this is not correct. It points to the object that you're calling it on; in this case, Function.prototype.
@pimvdb So what's the difference between object Function.prototype and object obj from my question?
@VladimirBezugliy When calling Function.prototype.method(), this points to an object, which doesn't have a prototype property. Type this in your REPL: ({}).prototype. The return value will be undefined.
0

prototype is only used when you are declaring the function, but not when you are calling it. Prototype makes the function a member of the object that gets created with every instance of that object.

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.