0

Coming from a background of C++,java-I do understand a little about classes.But replacing classes with prototype(as in javascript) seems to be a completely different thinking process. This maybe really difficult to grasp for novices like me-so I will be showing my own observation -which I got to after doing some deconstructing.

Classes in javascript is a relatively new concept,and from programmers from a java background-it would be best to explain to do the explaining in a way they are familiar with. Please enlighten me since Im bound to err,miss important points being a novice myself.

yasaaMoin
  • 31
  • 7

2 Answers2

1

Prototypal Inheritance:

An Object might be a Prototype for another Object. We can do that trough setting the prototype ( [[proto]] ) property:

var parent={
  name:"example"
}

var child={
  age:16
}

Object.setPrototypeOf(child,parent);

But why are Prototypes useful? Lets consider two cases:

  1. We set a property of an Object:

    1. The property is added to that object
  2. We try to get a value of a certain property:

    1. Check if the current Object has that property, if yes return its value, if not continue

    2. The current Object is set to Objects prototype, repeat with step 1

    3. If Object.prototype is kind of not defined ( we reached some kind of global object ) undefined is returned

Now lets take the upper code as an example:

  child.name;//"example" as its part of the prototype
  child.age;//its own property
  parent.name;//"example"
  parent.age;//undefined

And when we set a property:

child.name="child";
child.name;//child
parent.name//example

So thats how inheritance basically works. Its easy and not too difficult. To create an Instance, we can use Object.create or we can use the setPrototypeOf as shown in the upper example ( its quite new):

var child=Object.create(parent);//creates a new empty object with parent as prototype

Advanced Prototyping:

But what is missing with the upper code? Well, we want constructors! So we could simply create a function that assembles an Object (called factory function) with the prototype set:

function createChild(age){
   var child=Object.create(parent);
   child.age=age;
}

var child=createChild(15);

It has one problem: What if we want a childs child?

var thomasProto=Object.create(createChild(age));
thomasProto.name="Thomas";
function createThomas(age,someother){
   var instanceofThomas=Object.create(thomasProto);
   //how do we set age ?
   child.someother=someother
}

However this isnt really straightforward, thats why the new constructor was introduced.

It does sth like this:

  1. Create a new object with function.prototype as prototype

  2. Call function with this being the new Object

3: return the new Object

e.g:

function parent(){
   this.some="test";
}
parent.prototype={
   name:"example";
}

var child=new Parent();
child.some;//own value test
child.name;//inherits from parent.prototype

The prototype chain is:

child -> parent.prototype -> Object -> null

Class inheritance in JS

There is no class inheritance in JS.Theres just a class syntax that provides another way of creating prototypal inheritance.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • so thats what you meant by prototype pointing to another object-in other words that would mean prototype can be used for inheritance between two objects too. – yasaaMoin May 24 '17 at 14:13
  • @yasaaMoin not *too* but rather *only* – Jonas Wilms May 24 '17 at 14:18
  • "only"?-Then that would mean prototype is way more powerful than I thought.Also,thanks for the Advanced Prototyping part-Im learning a lot today – yasaaMoin May 24 '17 at 14:30
  • @yasaaMoin ever thought what {}.toString() , or "Hi!".repeat(5); are doing? Have a look at *Object.prototype.toString* and *String.prototype.repeat* . JS is based on prototypes... – Jonas Wilms May 24 '17 at 14:32
  • Out of curiousity-will I still be using prototypes if Im using functional programming? – yasaaMoin May 24 '17 at 15:14
  • 1
    @yasaaMoin functions have prototypes too.. Yes its nearly impossible to write code in js that does not use any kind of prototyping... – Jonas Wilms May 24 '17 at 15:17
  • thanks a lot,dude.You were a big help.Mind if I message you in the chat when Im in dire need of some explanation,regarding JS?Speaking in future tense,that is.. – yasaaMoin May 24 '17 at 15:26
  • 1
    @yasaaMoin: nope. Thats not my job. Please simply open another question on SO, im not the only JS guru out there... – Jonas Wilms May 24 '17 at 15:31
0

Here are my observations-.Enlighten me since I currently lack a lot of learning,and need to become better.


CLASS:

1.Consists of functions and variables.

2.Their contents can be inherited using 'extend' keyword.

3.We have parent class,child classes.

4.An object is something created using a constructor(i.e. a function).

5.An object inherits all the powers of the class the constructor came from.

6.An object inherits the ability to weild the variables,functions present in the class.

7.An object can inherit the powers of another class i.e. a class other than its own benefactor(=class).

8.A class ,by itself has no power-it can be well described as just a name given to a "group of people" or a civilization.The name is needed only for diplomatic reasons e.g: "extend"ing. Other than that ,all power to do anything exists with variables,functions.


prototype:

1.A prototype is an object->an object originally belonging to the function Object. Yes,"In the beginning there was 'only One function'", and yup-the rest of JavaScript functions descended from it.

2.A function can also be "a constructor",and its often called "a built-in object".

3.A method is nothing but a variable/property/object combined with a function.

4.A prototype is just a property which contains-functions,more properties.Thats how a property becomes "more than just a property"-it becomes an object.

5.But this object "prototype" is special,you see.The wise old Object provides it to all built-in,user-defined functions,objects to freely wield its contents,and its power of Inheritance.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
yasaaMoin
  • 31
  • 7
  • better: the prototype property points to another object. When an objectts key is resolved to a value, it first checks the current object if it has this, if not it goes to the prototype and repeats. If it reaches !obj.prototype or the key it stops – Jonas Wilms May 24 '17 at 13:54
  • your observations are basically made up from one or two examples. Inheritance is really complex and widely used in js... – Jonas Wilms May 24 '17 at 13:59
  • @Jonasw I do agree to that-since theres a lot I need to learn.But can you elaborate on "prototype pointing to ANOTHER object"? – yasaaMoin May 24 '17 at 14:02