Javascript: prototype inheritance and prototype property

I have a simple piece of code in JS that works with prototype inheritance.

function object(o) { function F() {} F.prototype = o; return new F(); } //the following code block has a alternate version var mammal = { color: "brown", getColor: function() { return this.color; } } var myCat = object(mammal); myCat.meow = function(){return "meow";} 

which worked fine, but adding the following:

 mammal.prototype.kindOf = "predator"; 

not. ("mammal.prototype - undefined")

Since I realized that the object does not have a prototype, I rewrote it, replacing var mammal = {... block:

 function mammal() { this.color = "brown"; this.getColor = function() { return this.color; } } 

which gave me a bunch of other errors:

"Function.prototype.toString called on an incompatible object"
and if I try to call _myCat.getColor ()
"myCat.getColor is not a function"


Now I'm completely confused. After reading Crockford and Flanagan, I did not get a solution to the errors. So it would be great if someone knew ...

- why the prototype is undefined in the first example (which is the main problem, I thought the prototype was explicitly set in the object () function)

- why should I try these strange errors to use the mammalian function as a prototype object in the object () function?

Edit by question creator: These two links also helped:

The prototypes_in_JavaScript in spheredev quiz explains how the prototype property works relatively simply. What he lacks are sample code examples. Some good examples are provided by the Morris John Article . I personally believe that the explanation is not as simple as in the first link, but still very good. The hardest part, even after I actually got it, is really not to confuse the .prototype property with the internal [[Prototype]] of the object.

+4
source share
1 answer

You get the first error (mammal.prototype - undefined) because the mammal is an object, the prototype property is added to function objects when they are created , and they should be used when you want to have function constructors .

I think that you are misleading this property with the internal [[Prototype]] property.

The [[Prototype]] property can be set only by the new operator, through the internal [[Construct]] operation.

This property is not available (although there are some ways, for example, in the implementation of Mozilla obj.__proto__; or the new ECMAScript 5 Object.getPrototypeOf ).

About the second question, you get these errors because you are creating a new object that inherits from the function and not from another object.

What about:

 var mammal = { color: "brown", getColor: function(){ return this.color; }, kindOf: "mammal" // "base" value }; // ... var tiger = object(mammal); tiger.roar = function(){return "roar";} tiger.kindOf = "predator"; // specific value 

In the above snippet, all instances inheriting from the mammal will have the kindOf property, which can later be changed when creating more specific mammalian objects.

Edit: In response to your comment, yes, the language itself gives you a tool to know that Object.prototype.hasOwnProperty , it returns a logical result indicating whether the object has a physically specified property or not, for example:

 var obj = { foo: 'bar' }; obj.hasOwnProperty('foo'); // true obj.hasOwnProperty('toString'); // false, inherited from Object.prototype 

You can also call this method directly on Object.prototype , so if someone calls the hasOwnProperty property for the object, it will not fail:

 Object.prototype.hasOwnProperty.call(obj, 'foo'); // true 
+5
source

Source: https://habr.com/ru/post/1304044/


All Articles