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
2 Answers 2
__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(){}
Comments
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.
2 Comments
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