JavaScript (JS) Bytes 2

JavaScript

JavaScript does not have "methods" in the form that class-based languages define them. In JavaScript, any function can be added to an object in the form of a property. An inherited function acts just as any other property, including property shadowing

//JS bytes Prototype
function Person(first,last,gender,age,interests) {
// this.first = first;
// this.last = last;
this.name = {
    first,
    last
};
this.age = age;
this.gender = gender;
this.interests = interests;
this.greeting = function(){
    console.log("Konichiwa  ! Watasiva namey wa ,"+ last + ' '+ first + " san desu!")
};    
}
Person.prototype.namaste = function(){
    console.log("Namaste  ! Watasiva namey wa ,"+ name.last + ' '+ name.first + " san desu!")
};

var person1 = new Person('Kim', 'Smith', 32, 'male', ['music', 'skiing']);

function Teacher(first, last, age, gender, interests, subject){
    Person.call(this, first, last, age, gender, interests);
    this.subject = subject;
}
Teacher.prototype = Object.create(Person.prototype);
Teacher.prototype.constructor = Teacher;
var teacher1 = new Teacher('John', 'Jones', 30, 'male', ['music', 'martial arts'],'kickboxing');

The performance of adding methods in prototype chain is beneficial, as the methods will not be reinitialized every time when an instance of the object is created. These are the standards specified in EcmaScript 2015

It is essential to understand the prototypal inheritance model before writing complex code that makes use of it. Also, be aware of the length of the prototype chains in your code and break them up if necessary to avoid possible performance problems. Further, the native prototypes should never be extended unless it is for the sake of compatibility with newer JavaScript features.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.