29/30 So Meta I Can't Take It! Can someone please explain what we are doing here?


#1


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

I think I quite grasped what objects are about. How to use the Class constructor and how to add methods and properties to instances of the class (e.g ObjectName.prototype.methodName = function() {}; ).

I also understand how to create instances, as well as some properties that come with objects such as typeof and hasOwnProperty but for the life of me, i cannot understand this comment from the lesson 29/30----->"If we have just a plain object (i.e., not created from a class constructor), recall that it automatically inherits from Object.prototype. Could this be where we get hasOwnProperty from? "<------


if we have a plain object for example: 

var Animal = {
      name: "Camel",
      hump: 2,
      legs: 4,
      terrain: land
};

from my understanding, the object Animal, is assigned it's own value. What does codecademy mean by saying "recall that it automatically inherits from Object.prototype"? what is the Object.prototype? are they talking about object inbuilt properties like properties like typeof and hasOwnProperties? The explanation isn't really clear to me.

Thanks for any help.


#2

Actually, typeof is not a property but an operator.

When we define a plain object,

plain_obj = new Object()  // constructor

or

plain_obj = {}            // literal

it has no prototype object of its own.

 > console.log(plain_obj.prototype)
 undefined
 > plain_obj.prototype.setName = function (newName) { this.name = newName; }
 TypeError: plain_obj.prototype is undefined

only a single prototype to inherit from, that of Object.

They both have the same constructor...

 > plain_obj.constructor
=> [Function: Object]

and when we look up the prototype chain we see {} and null

 > plain_obj.__proto__
=> {}
 > plain_obj.__proto__.__proto__
=> null

Object.prototype MDN

Constructor function objects also have a prototype object that their instances can inherit from. The neatest thing about this object is that any changes made to its attributes and methods are dynamically passed to all the instances. This will come up again and is good to know. When we change a constructor function after some instances are already in existence, those instances do not receive the change.

Inheritance and the prototype chain MDN


#3

Can you simplify this?


#4

It's been done on these forums. Dig around and you'll find lots of links. Then you can come back here and explain this to the rest of us. That would be cool, to say the least. The student becomes teacher. A perfect opportunity to sharpen your craft. Hope you are up to the challenge!


#5

Did some digging :slight_smile: came across some god sources, some good some..not so much.


#6

@mtf Does a good job explaining I found some more information here:

This really helped me understand a bit more: JavaScript Object Prototypes : W3Schools
"

JavaScript Object Prototypes

  • Every JavaScript object has a prototype.
  • The prototype is also an object.
  • All JavaScript objects inherit their properties and methods from their prototype

    JavaScript Prototypes

  • Objects created using an object literal, or with new Object(), inherit from a prototype called Object.prototype.

  • Objects created with new Date() inherit the Date.prototype.

  • The Object.prototype is on the top of the prototype chain.

  • All JavaScript objects (Date, Array, RegExp, Function, ....) inherit from the Object.prototype.
    "
    There rest you'd have to go to the resource and read up on.


#7

Yes! That's exactly my question! What is the Object.prototype that objects created using the literal notation inherit from?

Is the Object.prototype a kind of predefined JavaScript object?

I just want it in simple terms.. And I have checked the forums resources and the explanation is vague.

In addition, I have also moved on to the JS project now.


#8

even if it is not its own, as we've demonstrated.

prototype is first and foremost a prototype of an object. The basis of any prototype is {}.

So Object.prototype is really just mimicking the prototype of the object, {}.

This is where null comes in. It's the placeholder for that imaginary prototype. The top of the chain. It all fits when we wrap our head around it.

When we add classes we extend that chain with new prototypes to poll. They don't extend JS, though, only the objects in their scope, namely, their instances. This is the basis of OOP and where the rubber meets the road.


#9

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