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
Also in JavaScript:
- select parent of elemt
- js indexof string
- javascript resize event
- pause console debugger in react
- parse local json file
- javascript unshift
- duplicate images in webpage js
- user account in react
- how to give css style in javascript
- custom font in react native
- javascript get last character in string
- javascript loop through object properties
- js-cookie
- Javascript compare two dates
- javascript check empty object
- react native component at bottom center
- instanceof javascript
- h«javascript variable declaration
- js join a list of items with a number
- nodejs print
- Add object to array javascript
- how to put firebase config in a sperate file react native
- nativescript vue get native from ref
- generate random character in javascript