Javascript Inheritance

Description

A-Level Javascript (Inheritance) Note on Javascript Inheritance, created by James Drummond on 16/12/2014.
James Drummond
Note by James Drummond, updated more than 1 year ago
James Drummond
Created by James Drummond over 9 years ago
28
1

Resource summary

Page 1

Customer.prototype = new Person()What is happening here?

You are: Instantiating a new version of the parent object whose type is Object. Assigning the parent's external, accessible prototype property to the new Object's internal, inaccessible __proto__ property. Executing the Constructor function, using the newly created objects context whenever this is encountered. Returning the newly created object, unless the Constructor returns a non-primitive object, in which case the non-primitive object will be returned. Remember that the instance method will override the prototype method

What is the prototype in Javascript?- Remember that Javascript does not have classes - the idea of a 'template' that is instantiated into an object. A Javascript object either exists or not.- A Javascript *function* (not object) will have a prototype property. You cannot access this property directly - some browsers support the __proto__ property which you can access.A Javascript object has a: - prototype *property*- prototype You add functions and objects to the prototype object to:- Make them inheritable- Make it so that these exist independently of the original object. (If you assign obj to 'this', then only one version of obj exists which can be manipulated by all descendants.

Think of the prototype attribute as a characteristic of the object; this characteristic tells us the object’s “parent”. In simple terms: An object’s prototype attribute points to the object’s “parent”—the object it inherited its properties from.

Question. Inheritance is supposed to be: child.prototype = parent.prototype.So why, when you do this in practice do you need to say:child.prototype = new Parent(); ?You can do it this way but: you must call the constructor of the parent in the child constructor: function Child(name){ Parent.apply(this, args);}var sarah = new Child('Sarah');Why does Parent.apply(this, args); work?Answer: this is the same as saying: this.Parent(args); so you are calling the method Parent assigned to this.

The prototype property is an OBJECT. Male.prototype = { say : function(){}, run : function(){}}

The ‘prototype’ property points to the object that will be assigned as the prototype of instances created with that function when using ‘new’.

Object.create() method creates a new object with the specified prototype object and properties. The argument must be a prototype

What is the difference between:Child.prototype = Parent.prototypeandChild.prototype = new Parent(); // Only this maintains the prototypal chain. See: http://ejohn.org/apps/learn/#76Think about what you're saying here: you are saying that the prototype for both of these objects (and any descendants) is the same. So if any descendants modified a shared prototype method or property then *all* of the descendants would be affected.So if you did the same thing with another child, all of the objects would share the same prototype (see p.124 Stoyanov)

Here is the main thing:If you just assign a parent's prototype to the child (without calling new): The constructor will not be called. Therefore any methods and properties created using this in the constructor will not be inherited. The only things that will be inherited will be methods and properties that are part of prototype. new has not been called on the constructor which will probably have unintended consequences since this in the constructor will be the global object. So any methods / properties that you created in the constructor using this will be assigned to the global object - they will not be available to any children using this

Think of what happens when the constructor is called to know the difference between assigning a parent prototype to a child and assigning the result of a call to new. A new object is created and assigned to the child prototype this is assigned correctly to the new Object and any functions / methods

Show full summary Hide full summary

Similar

Biological Definitions
Yamminnnn
Types and Components of Computer Systems
Jess Peason
Using GoConqr to teach French
Sarah Egan
Using GoConqr to teach science
Sarah Egan
Using GoConqr to study geography
Sarah Egan
Using GoConqr to study Economics
Sarah Egan
Using GoConqr to study English literature
Sarah Egan
Using GoConqr to learn French
Sarah Egan
A Level: English language and literature techniques = Structure
Jessica 'JessieB
A Level: English language and literature technique = Dramatic terms
Jessica 'JessieB
Maths C4 Trig formulae (OCR MEI)
Zacchaeus Snape