So Meta I Can't Take It


#1


my code is 'fine' (guessed work but)...my comprehension of what I'm doing leaves a bit to be desired. Specific question, in trying to 'personalize' this exercise and try to alter the output, I'm quite flummoxed and even startled that a specific string statement is being recognized. IOW, why am I putting 'hasOwnProperty' into a string after declaring?? the hasOwnProperty (.hasOwnProperty('hasOwnProperty') function and even more importantly, what am I really establishing or why does it become false if I alter that string verbatim in anyway? To me it seems like .hasOwnProperty is then reduced to its string counterpart??

Apologize about any incoherence this question may have. I guess, asked more broadly, is it normal to feel little dizzy after initially learning OOP? Seems like 1001 somewhat arbitrary (realize thats probably naive perspective from still lack of big picture understanding) variations of logging objects/transferring properties etc...super fun, but also, super head spinny! lol


Replace this line with your code. 

// what is this "Object.prototype" anyway...?
var prototypeType = typeof Object.prototype
console.log(prototypeType);

// now let's examine it!
var hasOwn = Object.prototype.hasOwnProperty('hasOwnProperty');
console.log(hasOwn);


var wtf=typeof Object.prototype

console.log(wtf);

var okayy=Object.prototype.hasOwnProperty('canTellWhatIWrite');       -----TO ME, should yield same output as the redundant example above but comes out false! :0

console.log(okayy);


#2

At first you should know what is hasOwnProperty .
syntax:

obj.hasOwnProperty(prop)

Parameters

prop
The String name or Symbol of the property to test.

Return value

A Boolean indicating whether or not the object has the specified property.

hasOwnProperty checks if a property of an object belongs to the specified object, or is inherited via the prototype chain.
for example:
suppose we create a Object called o

var o = {a: 1};

here o is an object and a is a property on o.

so if we write like

o.hasOwnProperty('a') // true

it'll be true.
but if we write anything but a it'll be false.

o.hasOwnProperty('anything')//false

o.hasOwnProperty('aasdad') //false

The example i wrote just to check on the existence of a property on an object.but there are much more on it.if you properly know what the prototype is then it'll be easy to understand.if don't check it here
hasOwnProperty is a method on Object.prototype, which means it is available to all JavaScript objects.because each object has an internal link to another object called its prototype.

The real concept is hasOwnProperty method will be true only for direct properties not inherited one.

after creating the o Object if we create a Object like

var c = Object.create(o); // c is an object that inherits from o

and add a property

c.b='d'

here c is an Object and b is a direct property on c and a is also a property on c but inherited one.
console.log(c.a) //1
now check the result of hasOwnProperty

c.hasOwnProperty('b') //true`

c.hasOwnProperty('a') //false`

though a is a property on c but it'll false.

now i m not going to the details.coming to your question

the first hasOwnProperty is a method from Object.prototype.hasOwnProperty and second one is string value to check whether Object.prototype has the hasOwnProperty property as a direct property of that object.

it should be false .because Object.prototype has no own property namedcanTellWhatIWrite

var o = {a: 1};

// The newly created object o has Object.prototype as its [[Prototype]]
// o has no own property named 'hasOwnProperty'
// hasOwnProperty is an own property of Object.prototype. 
// So o inherits hasOwnProperty from Object.prototype
// Object.prototype has null as its prototype.
// o ---> Object.prototype ---> null

i hope you've understand now.feel free to ask :slight_smile:


#3

Hey, just wanna say thanks for such a thorough response! I will sift thru n digest it later and certainly ask if there is confusion in any of it. Very much appreciate the break down though. Thank you!

----- Reply message -----


#4

Just wanted to say I've 'sifted'... n more or less wrote your whole response down in my notes! lol. Really helpful walk-through on the process! Thank you!


#5

Okay...so still playing around in effort of trying to have things click and, ughhh. No luck! Lol...Your explanation makes alot of sense so far as direct/inherited properties and testing for values is concerned but so far as my own experience, it seems Object.prototype and hasOwnProperty are still strictly self-serving (I can't add properties to whatever variable I assign Object.prototype and hasOwnProperty only recognizes itself in string form.)

Its worth saying I'm spitballing my syntax entirely as again, OOP so far has kind of overwhelmed me with its what appears as somewhat arbitrary variations. Reminds me of Karate Kid in painting the fence and waxing on and off, not sure what any of it has to do with the big picture until...lol.

So if you could just provide a syntax example of getting a true statement with hasOwnProperty aside from the redundant example of hasOwnProperty('hasOwnProperty'). That would hopefully help! Thanks!


#6

At first i want to ask did you check the link what i gave you?
did you understand what is prototype?

can you please elaborate this with syntax example?

var myCar = new Object(); //myCar is a Object
myCar.make = "Ford"; // make is a property of myCar
myCar.model = "Mustang";// model is a property of myCar
myCar.year = 1969; // year is a property of myCar

console.log(myCar.hasOwnProperty('make'))//true
console.log(myCar.hasOwnProperty('model'))//true
console.log(myCar.hasOwnProperty('year'))//true
console.log(myCar.hasOwnProperty('name'))//false(name is not a property of myCar)

now remember all the name of properties should be with quotes.because
hasOwnProperty expects the property name as a string.When you call
myCar.hasOwnProperty(make) you are passing it the value of the make
variable (which doesn't exist).

Object.prototype.hasOwnProperty('hasOwnProperty')//true
myCar.hasOwnProperty('hasOwnProperty')//false

inside of quotation marks hasOwnProperty is just a string value like other properties of myCar(make,model,year).
This method does not check if the property exists in the object's prototype chain, the property must be a member of the object itself.Object.prototype itself is an object! And hasOwnProperty is direct property on Object.prototype.so it'll return true.though all objects have the hasOwnPropertymethod but it's not direct property on myCar so it'll return false.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.