Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the function declared in the prototype called?

var p = function () {
    this.show = function () {
       alert('hello world!!!');
    }
}

p.prototype.show = function() {
    alert('haha');
}

var o  = new p();
o.show();

It alerts "hello world!!!", why?

Can I modify prototype method, if yes how?

like image 911
smilexu Avatar asked Dec 05 '12 08:12

smilexu


People also ask

What is not included in function prototype?

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body.

Where must a function prototype be declared?

In order to properly call a variadic function in C ( printf for example) the function must be declared with a prototype before the point of the call. Otherwise, the behavior is undefined.

Is function declaration function prototype?

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.

Is function declaration and prototype the same?

A function declaration is any form of line declaring a function and ending with ; . A prototype is a function declaration where all types of the parameters are specified.


3 Answers

That's because the specific function you define in the constructor overrides the one that is inherited through the prototype.

From EcmaScript specification :

Every object created by a constructor has an implicit reference (called the object’s prototype) to the value of its constructor’s “prototype” property. Furthermore, a prototype may have a non-null implicit reference to its prototype, and so on; this is called the prototype chain. When a reference is made to a property in an object, that reference is to the property of that name in the first object in the prototype chain that contains a property of that name. In other words, first the object mentioned directly is examined for such a property; if that object contains the named property, that is the property to which the reference refers; if that object does not contain the named property, the prototype for that object is examined next; and so on.

In short : when looking for a function (or any property by its name), you start at the object and then go up in the prototype chain.

like image 105
Denys Séguret Avatar answered Oct 19 '22 20:10

Denys Séguret


You override your prototype.show method in p function.

like image 3
Boris Pavlovic Avatar answered Oct 19 '22 19:10

Boris Pavlovic


In Javascript when a property is resolved the engine first looks at the properties of an object. In your example the object would be represented by this. If it finds the property, in this case show (Remember functions can be properties) it uses that property. If the property is not found it then iterates down the prototype chain in an effort to resolve the property.

like image 1
Kevin Bowersox Avatar answered Oct 19 '22 19:10

Kevin Bowersox