29. So Meta I Can't Take It! - Printing correct output?


#1

Hey, just wondering if the console is logging the correct things here! Thanks

Exercise: https://www.codecademy.com/courses/objects-ii/5/3?curriculum_id=506324b3a7dffd00020bf661

This is what the console logs:
object
true

// 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);


#2

I'm assuming, I'm getting the same thing,


#3

@travism87
@satty10

/*
A prototype is an early sample, model, or release of a product
built to test a concept or process or
to act as a thing to be replicated or learned from.
The word prototype derives from the Greek πρωτότυπον prototypon,
"primitive form", neutral of πρωτότυπος prototypos,
"original, primitive", from πρῶτος protos, "first" and τύπος typos, "impression".

*/

/*
Inheritance and the Prototype Chain
http://stackoverflow.com/questions/19633762/classical-inheritance-vs-protoypal-inheritance-in-javascript
http://stackoverflow.com/questions/2800964/benefits-of-prototypal-inheritance-over-classical
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
*/

/*
Prototypal Inheritance is Dynamic

One of the most important advantages of prototypal inheritance is
that you can add new properties to prototypes after they are created.
This allows you to add new methods to a prototype
which will be automatically made available
to all the objects which delegate to that prototype.

This is not possible in classical inheritance
because once a class is created you can't modify it at runtime.
This is probably the single
biggest advantage of
prototypal inheritance
over classical inheritance,
and it should have been at the top.

Conclusion

Prototypal inheritance matters.
It's important to educate JavaScript programmers
on why to abandon the constructor pattern of prototypal inheritance
in favor of the prototypal pattern of prototypal inheritance.

We need to start teaching JavaScript correctly
and that means showing new programmers how to write code
using the prototypal pattern instead of the constructor pattern.

Not only will it be it easier to explain
prototypal inheritance using the prototypal pattern,
but it will also make better programmers

*/
/*
++++++++ a String type explained ++++++

why it should be Object.prototype.hasOwnProperty....
The hasOwnProperty Method is the only method
which will be fetched from the Object.prototype WITHOUT traversing
through the prototype Chain.

In the following link:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain
Under the Header:
Performance

The lookup time for properties that are high up on the prototype chain can have a negative impact on performance, and this may be significant in code where performance is critical. Additionally, trying to access nonexistent properties will always traverse the full prototype chain.

Also, when iterating over the properties of an object, every property that is on the prototype chain will be enumerated.

To check whether an object has a property defined on itself and not somewhere on its prototype chain, it is necessary to use the hasOwnProperty method which all objects inherit from Object.prototype.

hasOwnProperty is the only thing in JavaScript which deals with properties and does not traverse the prototype chain._

========================================

now to your case:
var prototypeType = typeof Object.prototype;
will leave you with a variable which has a VALUE of the string-type

As the string is created, you will get a present...the length Property
prototypeType.hasOwnProperty("length") ==> true.

From the String.prototype you will inherit the methods:
'sup',
'sub',
'strike',
'small',
'link',
'italics',
'fontsize',
'fontcolor',
'fixed',
'bold',
'blink',
'big',
'anchor',
'valueOf',
'trim',
'toUpperCase',
'toString',
'toLowerCase',
'toLocaleUpperCase',
'toLocaleLowerCase',
'substr',
'substring',
'split',
'match',
'localeCompare',
'concat',
'charCodeAt',
'charAt',
'slice',
'search',
'replace',
'lastIndexOf',
'indexOf'

And as anything in the Javascript environment is an object
from the Object.prototype you inherit:
'__lookupSetter__',
'__lookupGetter__',
'__defineSetter__',
'__defineGetter__',
'__proto__',
'toLocaleString',
'isPrototypeOf',
'propertyIsEnumerable',
'hasOwnProperty' \<== this is a special one !!

*/


#4

You mean this?

Instead of :

function Person(name,age){
this.name=name;
this.age=age;
}

using the following?!

function Person(){

}

Person.prototype.name=this.name;
Person.prototype.age=this.age;

Thank you very much


#5

@zoe1992,
And then every Instance of Person
having the same name and age ????
and having hasOwnProperty being false.....


#6

ahhh i follow now really didn't understand what the question was asking
many thanks, i was stuck on this one to but this helped me work out what they wanted.


#7

Hey,
I still haven't really figured it can you try to explain me in your words this 2 cases in the code in a 'beginner' words. thanks :slight_smile: i know you are new too that's why im asking you thanks again


#8

I had to study it for about an hour or so, and I can't easily explain it well, but if you study these, they should help you make sense of it:

Begin by referring to exercise 7 in the same lesson (Introduction to Objects II)
w3schools.com - JavaScript Object Prototypes - start here

Then read these (they are what made the concept really stick for me):
Mozilla Developer Network - Inheritance and the prototype chain
Mozilla Developer Network - Object.prototype.hasOwnProperty()


#9

In addition to Travism87's helpful post, the following article helped me also understand the hasOwnProperty Method.

MSDN.Microsoft.com: hasOwnProperty Method article