* JavaScript implements inheritance by using objects. Each object has an internal link to another object called its prototype. * Prototypes are hidden objects that are used to share the properties and methods of a parent class to child classes. Live result:- https://playcode.io/1943770 /* Find the age greater than 10 from an array of age */ var age = [ 10 , 11 , 2 , 1 , 20 ]; /*Solution 1 with .filter() function*/ let result = age . filter ( function ( a ){ return a > 10 }); console . log ( 'Age greater than 10 : ' , result ); /*Solution 2 using custom prototype method ' showAgeGreaterThan10' */ Array . prototype . showAgeGreaterThan10 = function () { return ( age . filter ( function ( val ){ return val > 10 }) ) }; console . log ( 'Custom prototype :' , age . showAgeGreaterThan10 ());
In JavaScript, this is often achieved using methods like - Object.assign({}, originalObject) or {...originalObject} Live result URL:- https://playcode.io/1944513 //Deep and Shallow Copy in JavaScript //Example 1 let x = 'Hello world' ; let y = x ; console . log ( "x is : " , x ); console . log ( "y is : " , y ); //Example 2 let obj = { name : 'Anil' } let obj1 = obj ; obj1 . name = 'Reena' ; console . log ( "obj is : " , obj ); console . log ( "obj1 is : " , obj1 ); //Example 3 let objA = { name : 'Anil A' } let objB = Object . assign ({}, objA ); //Type 1: this is called a 'Shallow Copy'. It copies the memory location. Also, it copies only the main object. //OR let objB ={...objA} //Type 2: this is also called a 'Shallow Copy'. It copies the memory location. Also, it copies only the main object. objB . name = 'Reena B' ; console . log ( "objA is : " , objA ); c