Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript Add Methods to a Constructor Function Using Prototype

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23
}

// creating objects
const person1 = new Person();
const person2 = new Person();

// adding a method to the constructor function
Person.prototype.greet = function() {
    console.log('hello' + ' ' +  this.name);
}

person1.greet(); // hello John
person2.greet(); // hello John
Comment

PREVIOUS NEXT
Code Example
Javascript :: Babel, env src decrypt, React into javascript in background 
Javascript :: javascript reduce mdn 
Javascript :: ticket 
Javascript :: Load RequireJS Script 
Javascript :: enzyme debounce test 
Javascript :: js watchFile 
Javascript :: modify summernote with js 
Javascript :: convert snake case to camelcase javascript recursive 
Javascript :: vertical lineal star pattern javascript 
Javascript :: pass prop through route 
Javascript :: dropdown list trigger change with value jquery 
Javascript :: delete all document fragments js 
Javascript :: formatting time for ical export 
Javascript :: js create an object from another object with some keys removed 
Javascript :: async data nuxt multiple requests 
Javascript :: Change slick slider slides from another component 
Javascript :: jquery to javascript converter online free 
Javascript :: deep copy array of objects javascript 
Javascript :: get window object in nextjs 
Javascript :: grouping related html form input 
Javascript :: using multiple dropzones with formik field array 
Javascript :: wordpress how to read jquery 
Javascript :: Change tilte alert 
Javascript :: terraform for loop json 
Javascript :: class angular dynamic template 
Javascript :: javascript For some reason my content within an array is not printing out to the screen 
Javascript :: Why is this forEach code snippet invalid in AngularJS 
Javascript :: Easy Angular way to detect if element is in viewport on scroll 
Javascript :: How to map a JSON response with different indexes 
Javascript :: javascript array add method 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =