Lesson 29


#1



Okay, I'm stumped, dyslexic and eroding my sanity with this one. Where am I going wrong? Am I simply too dense syntactically to see my own problems? Help!

29. So Meta I can't take it

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

My console output reads:
object
false

But the error message reads:
Oops, try again. Make sure to set prototypeType to the typeof Object.prototype
...but I thought I had already. Right?


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

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


#2

@hollygef,

/*
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 !!

*/


#3

var hasOwn = Object.prototype.hasOwnProperty('hasOwnProperty');


#4

this worked for me

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

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