0

When I execute below java script code I get error at "v.dummy();" line , please let me know where am I doing wrong.

 function Test()
 {
 }
 Test.prototype.foo = function () {
 console.log('foo');
 }
 var v = new Test();
 v.foo();
 v.__proto__ = function dummy() {
 console.log('__proto__'); 
 };
 v.dummy(); // Uncaught TypeError: v.dummy is not a function
asked Dec 3, 2015 at 13:47

2 Answers 2

1

__proto__ is just a reference of an object

IMG

You can't make it equal a new function, but you can do it like this:

v.__proto__.foo = function dummy(){}
brasofilo
26.2k15 gold badges96 silver badges189 bronze badges
answered Dec 3, 2015 at 13:58
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know what you are trying to do, but:

1) when you assign function to a variable, you may omit the name (dummy) and use an anonymous function (without a name). Function name is useless in this case. And if you do

var x = function y(){ ... }

you can call it like this: x(), not (削除) y() (削除ここまで)

2) __proto__ should be an object, not a function

usage of foo is correct, therefore it works.

You may want to consider reading a good JS book.

answered Dec 3, 2015 at 14:02

2 Comments

I made below change and it worked v.__proto__ = { dummy: function () { console.log('proto'); } }; My assumption was function is also an object in java script. May be as you said i need to read JS good parts.
@CleanCrispCode Yes, function is an object in js. In your code you can call your function with v.__proto__(). But you should NOT do this, because __proto__ is an inner object used by javascript (to search the prototype chain to resolve methods it cannot find in the object itself). There are a lot more that can be told about prototypes, therefore I recommended a book

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.