Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SuperHero.prototype = Object.create( Person.prototype );
- var Person = function( firstName , lastName ){
- this.firstName = firstName;
- this.lastName = lastName;
- this.gender = "male";
- };
- // a new instance of Person can then easily be created as follows:
- var clark = new Person( "Clark" , "Kent" );
- // Define a subclass constructor for for "Superhero":
- var Superhero = function( firstName, lastName , powers ){
- // Invoke the superclass constructor on the new object
- // then use .call() to invoke the constructor as a method of
- // the object to be initialized.
- Person.call( this, firstName, lastName );
- // Finally, store their powers, a new array of traits not found in a normal "Person"
- this.powers = powers;
- };
- SuperHero.prototype = Object.create( Person.prototype );
- var superman = new Superhero( "Clark" ,"Kent" , ["flight","heat-vision"] );
- console.log( superman );
- // Outputs Person attributes as well as powers
- SuperHero.prototype = new Person();
- SuperHero.prototype = Object.create( Person.prototype );
- SuperHero.prototype = new Person();
- var person = new Person();
- SuperHero.prototype = person; // inheritance here
- // the Object.create way is ECMAScript 5 (which not all browsers support yet sadly)
- var superman = new SuperHero( "Clark" ,"Kent" , ["flight","heat-vision"] );
- console.log( person );
- console.log( superman );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement