Found 9297 Articles for Object Oriented Programming

Can we share a method between JavaScript objects in an array?

AmitDiwan
Updated on 18-Jul-2020 07:58:02

91 Views

Following is the code to share a method between objects in an array −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Share a method between objects in an array CLICK HERE Click on the above button to call welcome() method of all person objects    let resEle = document.querySelector(".result");    function Person(firstName, lastName) {       this.firstName = firstName; ... Read More

Binding an object's method to a click handler in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:58:14

197 Views

Following is the code for binding an object’s method to a click handler in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Binding an object's method to a click handler in JavaScript Click Here Click on the above button to know how many times you have clicked the button    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let testObj = {       a: 0,       timesClicked() {          this.a += 1;          resEle.innerHTML = "Times clicked = " + this.a;       },    };    BtnEle.addEventListener("click", () => {       testObj.timesClicked();    }); OutputOn clicking the ‘Click Here’ button −

Can we modify built-in object prototypes in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:56:14

134 Views

Following is the code for modifying built-in object prototypes in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Modify built-in object prototypes Click Here Click on the above button to call the alert method    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    window.alert = function (msg) {       resEle.innerHTML = "Custom Alert function has been triggered";       resEle.innerHTML += "Alert Message = " + msg;    };    BtnEle.addEventListener("click", () => {       alert("Hello World");    }); OutputOn clicking the ‘Click Here’ button −

De-structuring an object without one key

AmitDiwan
Updated on 18-Jul-2020 07:55:36

102 Views

Following is the code to de-structure an object without one key −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } De structuring an object without one key. {"firstName":"Rohit", "lastName":"Sharma", "age":22} CLICK HERE Click on the above button to destructure the above object    let resEle = document.querySelector(".result");    let obj ... Read More

Adding methods to Javascript Prototypes

AmitDiwan
Updated on 18-Jul-2020 07:54:11

66 Views

Following is the code for adding methods to JavaScript prototypes −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Adding methods to Javascript Prototypes Click Here Click on the above button to call the methods of person1 and person2 object    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function personConstructor(fName, lName, birthYear) {       this.fName ... Read More

Function prototypes in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:52:35

159 Views

Functions created in JavaScript always have the prototype property added by the JavaScript engine to them. The prototype property is an object which contains the constructor property by default. The function protoype can be accessed by −functionName.prototypeWhen objects are creating using the function constructor, this prototype property can be used to share methods or properties between the objects created by that function constructor.Following is the code for function prototypes in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       ... Read More

Anonymous Wrapper Functions in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:50:17

581 Views

Anonymous functions are used to wrap code snippets, JavaScript libraries, functions etc to control their visibility and the namespace so that there shouldn’t be any conflict with other libraries code. IIFE (Immediately Invoked Function Expressions) are used for this purpose.Following is the code to implement Anonymous Wrapper Functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Anonymous wrapper functions in JavaScript 0    let resEle = document.querySelector(".result");    (function () {       resEle.innerHTML =       "This piece of code is automatically executed as it is inside an IIFE";    })(); Output

Sum of nested object values in Array using JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:53:21

811 Views

Following is the code to sum nested object values in array using JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Sum of nested object values in Array CLICK HERE Click on the above button to sum the nested object values of json array    let json = {       storeData: [       { ... Read More

Implement Private properties using closures in JavaScript

AmitDiwan
Updated on 18-Jul-2020 07:48:37

97 Views

Following is the code to implement private properties using closures in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Private properties, using closures 0 CLICK HERE Click on the above button to increment the above counter using closures    let BtnEle = document.querySelector(".Btn");    let resEle = document.querySelector(".result");    function test() {       let a = 0;       return function incrementA() {          a++;          return a;       };    }    let storeVal = test();    BtnEle.addEventListener("click", (event) => {       resEle.innerHTML = storeVal();    }); OutputOn clicking the ‘CLICK HERE’ button the counter will increase on each click −

Using ‘{ }' in JavaScript imports?

AmitDiwan
Updated on 18-Jul-2020 07:47:41

90 Views

Following is the code using {} in javaScript imports −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Using '{ }' in javascript imports CLICK HERE Click on the above button to execute the imported function script.jsimport {test, tellTime as showTime} from "./sample.js"; let resultEle = document.querySelector('.result'); document.querySelector('.Btn').addEventListener('click', ()=>{    resultEle.innerHTML+=test();    resultEle.innerHTML+=showTime(); })sample.jsfunction testImport() {    return "Module ... Read More

Advertisements