xxxxxxxxxx
//every object has internal property call prototype.
//prototype is simple reference to another object that
//contain common attributes and methods which will save memeory
function Todo(name, completed) {
this.name = name;
this.completed = completed;
this.getTodoNameInsideObject = function () {
console.log(this.name);
};
}
Todo.prototype.getTodoName = function () {
console.log(this.name);
};
const todo = new Todo('Buy Eggs', false);
const todo2 = new Todo('Learn javascript', false);
todo.getTodoName();
console.log(todo, todo2);
console.log(todo.getTodoNameInsideObject === todo2.getTodoNameInsideObject);
//log false as it is not same function☻
console.log(todo.getTodoName === todo2.getTodoName);
//log true as it is same function
//at console getTodoName will be inside prototype which will save memeory.
const arr = [1, 2, 3, 4, 5];
console.log(typeof arr);
console.log(arr);
//arr is object and we can use array method as it is in prototype like push
arr.push(6);
xxxxxxxxxx
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var person = new Person("John Doe");
person.getName() //"John Doe"
xxxxxxxxxx
* Prototype
-Every object in JavaScript has a built-in property,
which is called its prototype.
-The prototype is itself an object, so the prototype will
have its own prototype, making what iss called a prototype chain.
-The chain ends when we reach a prototype that has null for
its own prototype.
-Prototypes are the mechanism by which JavaScript objects inherit
features from one another
const arr = [1,2,3,4]
// proto type : arr.__proto__
// prototype chain : arr.__proto__.__proto__.__proto__
xxxxxxxxxx
/*Prototype is used to add properties/methods to a
constructor function as in example below */
function ConstructorFunction(name){
this.name = name //referencing to current executing object
}
ConstructorFunction.prototype.age = 18
let objectName = new ConstructorFunction("Bob")
console.log(objectName.age) //18
xxxxxxxxxx
// prototypes in javascript, THey create new Methods to our constructor function
const EmployersInfo = function(name , salary , bonusPercentage){
this.name = name ;
this.salary = salary;
this.bonusPercentage = bonusPercentage;
}
// prototype for getting the employer credential
EmployersInfo.prototype.credentialInfo = function(){
return `${this.name} Has a base salary of ${this.salary}`;
}
EmployersInfo.prototype.getBonus = function(){
let bonusAmount = (this.salary * (this.bonusPercentage));
return `${this.name} earned ${bonusAmount} Bonus`;
}
// let's first create the instance of employerInform
const employerInform = new EmployersInfo('kevin' , 12000 , 12);
// logging employerInform
console.log(employerInform);
// logging the credentials
console.log(employerInform.credentialInfo());
// logging the Bonus function
console.log(employerInform.getBonus());
2
Show 6 More Grepper Results
xxxxxxxxxx
var proto = { describe: function () { return 'name: '+this.name; }};var obj = { [[Prototype]]: proto, name: 'obj'};
xxxxxxxxxx
//every object has internal property call prototype.
//prototype is simple reference to another object that
//contain common attributes and methods which will save memeory
function Todo(name,completed){
this.name=name;
this.completed= completed;
}
Todo.prototype.getTodoName= function(){
console.log(this.name)
}
const todo= new Todo("Buy Eggs", false);
const todo2= new Todo("Learn javascript", false);
todo.getTodoName();
console.log(todo,todo2);
//at console getTodoName will be inside prototype which will save memeory