Lecture08-Inheritance.pdf - JavaScript Inheritance...

This preview shows 1 out of 7 pages.

JavaScript Inheritance
Image of page 1

Subscribe to view the full document.

Inheritance JavaScript is object oriented but without classical inheritance Inheritance is achieved via the prototype chain . Objects get access to properties and methods of their prototype object. Object is the end of the prototype chain. // a.__proto__ is Object var a = {}; // b.__proto__ is function // b.__proto__.__proto__ is Object var b = function (){}; // c.__proto__ is array // c.__proto__.__proto__ is Object var c = [];
Image of page 2
Objects & Prototypical inheritance var a = { x : 10 , calculate : function (z) { return this . x + this . y + z; } }; var b = { y : 20 , __proto__ : a }; var c = { y : 30 , __proto__ : a }; // call the inherited method b . calculate ( 30 ); // 60 c . calculate ( 40 ); // 80
Image of page 3

Subscribe to view the full document.

Object.create() ES5 standardized an alternative way of prototype-based inheritance using Object.create () method. It sets __proto__ property to original object for inheritance. var person = { first : 'Default' , last : 'Default' , greet : function () { return 'Hi' + this.first ; } //use this in functions } var jim = Object.create( person ); console.log ( jim[ 'first' ] ); // Default - Inheritance console.log ( jim.hasOwnProperty( 'first' ) ); // false jim.first = 'Jim' ; console.log ( jim.hasOwnProperty( 'first' ) ); // true console.log ( jim ); // {first: 'Jim'} No last & greet() jim . greet (); // Hi Jim
Image of page 4
JavaScript Object properties A JavaScript object is a collection of unordered properties. Properties can usually be changed, added, and deleted, but some are read only. The delete operator is designed to be used on object properties. It has no effect on variables or functions. The delete operator should not be used on predefined JavaScript object properties. It can crash your application. JavaScript object inherit the properties of their prototype The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from a prototype.
Image of page 5

Subscribe to view the full document.

JavaScript Object properties var student = { name : "Jim Carrey" , course : "WAP" , no : 12 }; for ( var key in student ) { console . log ( key
Image of page 6
Image of page 7
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern