Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript prototype vs constructor function

function Class () {}
Class.prototype.calc = function (a, b) {
    return a + b;
}

// Create 2 instances:
var ins1 = new Class(),
    ins2 = new Class();

// Test the calc method:
console.log(ins1.calc(1,1), ins2.calc(1,1));
// -> 2, 2

// Change the prototype method
Class.prototype.calc = function () {
    var args = Array.prototype.slice.apply(arguments),
        res = 0, c;

    while (c = args.shift())
        res += c;

    return res; 
}

// Test the calc method:
console.log(ins1.calc(1,1,1), ins2.calc(1,1,1));
// -> 3, 3
Comment

PREVIOUS NEXT
Code Example
Javascript :: typescript deserialize json 
Javascript :: how to build and deploy a react app to github pages 
Javascript :: define component react with default props and props type 
Javascript :: take a screenshot javascript of canvas 
Javascript :: Nodemailer Google Passport Oauth Strategy 
Javascript :: big.js 
Javascript :: if array javascript 
Javascript :: typeorm in 
Javascript :: js Destructuring arrays and objects 
Javascript :: calling javascript from java 
Javascript :: notification react native 
Javascript :: javascript weakmap 
Javascript :: value js 
Javascript :: create and save xml file in javascript 
Javascript :: js listen websocket 
Javascript :: react navbar responsive 
Javascript :: vuex store in js file 
Javascript :: var hoisting.js 
Javascript :: calendar picker react js 
Javascript :: hoisting in javascript mdn 
Javascript :: .then(async 
Javascript :: npm start browser 
Javascript :: Get a random value from an array in JS 
Javascript :: anonymous functions in javascript 
Javascript :: react catch error in component 
Javascript :: alert modal 
Javascript :: ts base64 from file 
Javascript :: Sequelize using javascript 
Javascript :: var = {} js 
Javascript :: convert json data into html table 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =