Owned by a builder

1

The prototype of a constructor is a function. So, why in this case do you throw false ?

function A() {
  this.n = 15;
}

A.prototype = {b: 2, c: 3};

Function.prototype.nn = 15;

console.log(Object.getPrototypeOf(A));

console.log(A.hasOwnProperty("n")); // false porque está heredado

console.log(A.hasOwnProperty("b")); // Falso porque declaramos su prototipo

console.log(A.hasOwnProperty("nn"));  // False?? por que?

My problem is in the last console.log() . What is the error in my concepts?

    
asked by Eduardo Sebastian 04.07.2017 в 17:37
source

2 answers

4

We will give as an example the following code:

function A() {
  this.atr1 = 1;
};

A.prototype = {atr2: 2, atr3: 3};

Function.prototype.atr4 = 4;

var a = new A();

In this code we have the following objects:

  • Constructor A : is a function that will be used as a constructor to create instances of new objects. Although being a function is also an object.
  • A.prototype : is the prototype object of A that is created automatically when you define function A. This object will be the ascending __proto__ of any object instance created from from the constructor A.
  • Object to : is an instance of an object created from constructor A. This object will initially have attributes defined with this at the time of creation with new A() since it executes the function A() with this the object a newly created. Automatically the __proto__ of this object will be A.prototype .
  • Function : It is the constructor function used implicitly in the creation of any function. Therefore, the prototype of Function ( Function.prototype ) is automatically assigned as the __proto__ of any created function.

When trying to access an attribute of an object, JS first looks at whether that attribute is inside the object itself. If you can not find it, then start tracing the __proto__ parent successively until you find the attribute.

In this example, if I try to access a.atr2 , since this attribute is not within a but yes in the __proto__ of a ( A.prototype ), it would return its value: 2.

The hasOwnProperty() function checks if a property is inside an object, but only inside the object. If the property is not inside the object but is accessible through its __proto__ , the function hasOwnProperty() would return false .

Therefore, in your code you are trying to check if the A() object has a series of direct properties and, as you can see, none of them is within A() .

For that you should have done something like this: A.atr1 = 10 . Here atr1 would be an object property A() , and return true if A.hasOwnProperty("atr1") is made.

Stop by here to better understand the subject of objects in JS. link

    
answered by 04.07.2017 / 21:49
source
0

hasOwnProperty is a method that is used specifically for objects, in your example A is just a function, you have to instantiate it so that hasOwnProperty of the results you expect, this because the line console.log(A.hasOwnProperty("n")); must give true because it is a direct property of the object.

Your last line is false because hasOwnProperty only checks if the object itself has that property, any other defined from the prototype is still inherited and that's why it sends you false, for Javascript to find it and you have to use true the operator in

function A() {
	this.n = 15;
}

A.prototype = {
	b: 2,
	c: 3
};

var test = new A();

Function.prototype.nn = 15;

Object.prototype.bb = 305;

console.log("test property n: "+test.hasOwnProperty("n")); // true porque es propiedad directa

console.log("test property b: "+test.hasOwnProperty("b")); // Falso porque declaramos su prototipo

console.log("text property nn: " + test.hasOwnProperty("nn")); //False porque la propiedad se definió para Function y no para Object

console.log("text property bb: " + test.hasOwnProperty("bb")); //False porque hasOwnProperty no busca en el prototipo de manera extensiva

console.log("nn in text: " + ("nn" in test)); //False porque la propiedad se definió para Function y no para Object, aún usando el operador 'in'

console.log("bb in text: " + ("bb" in test)); //True porque el operador 'in' busca de manera extensiva
    
answered by 04.07.2017 в 18:07