• When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.
  • The Function constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues to eval. However, unlike eval, the Function constructor creates functions which execute in the global scope only.
  • Properties and Methods of Function 
    The global Function object has no methods or properties of its own. However, since it is a function itself, it does inherit some methods and properties through the prototype chain from Function.prototype.
var john ={
    name: 'John',
    yearOfBirth: 1990,
    job: 'teacher'
};

var Person = function(name, yearOfBirth, job) {
    this.name = name;
    this.yearOfBirth = yearOfBirth;
    this.job = job;
};

Person.prototype.calculateAge =
function(){
    console.log(2016-this.yearOfBirth)
};
Person.prototype.lastName = 'Smith';
// evrey obj can use this function/properties cuz it attached to prototype.
//---> Inheritance




//this is how we creat new obj using function constructor
var john = new Person('John', 1990, 'teacher');//instantiation of Person obj
var jane = new Person('Jane', 1969, 'designer');
var mark = new Person('Mark', 1948, 'retired'); 


john.calculateAge();
jane.calculateAge();
mark.calculateAge();

console.log(john.lastName);
console.log(jane.lastName);
console.log(mark.lastName);

 

 

'javascript' 카테고리의 다른 글

JavaScript/ First-Class Functions  (0) 2020.02.24
JavaScript/ Primitives vs Objects  (0) 2020.02.24
JavaScript/ The 'this' keyword  (0) 2020.02.24
JavaScript/ Scoping/ 변수영역  (0) 2020.02.24
JavaScript/ Hoisting, Function and Variable  (0) 2020.02.14