I'm learning prototypes, Object.assign but something went wrong

const focus = {
    focus:()=>console.log("focus")  //focus method
}
function HtmlElement() {
 this.click=()=> console.log("clicked");  //click method 

}

Object.assign(HtmlElement.prototype,focus);   
//  i'm trying to copy focus
// method to HtmlElement

function HtmlSelectElement(){
    let k=[];
    this.addItem=(item)=>k.push(k);  //push the elements
    this.removeItem=(item)=>{        //removes the elements
        for(let i in k){
            if(k[i]===item) {
                k.splice(i,i);
            }
        }
    }
    Object.defineProperty(this,'k',{   //getter to print the final elements in k
        get : function(){
        return k;
        }
    })
}
//VEry import NOte: if we do HtmlSelectElement.prototype= Object.HtmlElement.prototype
//it only copies object.htmlelement.prototype methods like focus but 
//it won't copy the click method
//to slove this we have this thing.. as below
HtmlSelectElement.prototype= new HtmlElement();  //copying all members and methods to HtmlSelectElement
let ob1=new HtmlSelectElement(); 
ob1.addItem('1');
ob1.addItem('2');
console.log(ob1.k);
 

Output: <ref *1> [ [Circular *1], [Circular *1] ]

Sir, i changed it but it same error :frowning:

You’re pushing the array k into the array k

1 Like

yes ur right thank u <3

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.