Philll_t
Philll_t

Reputation: 4437

Making a custom method that changes an objects property

I'm new to custom objects but have found them very useful, especially because the reduce a lot of code writing in the long run.

I'm working a particular algorithm that creates an clone element and uses a method that creates a new unique ID based on some properties of the cloned element. this is what it kinda looks like right now:

Element.prototype.theID = theID;

function theID(){
//this.makeNewID code
//code that builds a new ID and stores it in a variable called nwID
return nwID
}

function buildClone(cloneThis){
//builds  a clone out of the specified cloneThis element and stores it in a variable
//called clonedElement
 var nwID = clonedElement.theID;//determines a new ID
 clonedElement.setAttribute('id', nwID);//asignes the ID to the element.
 }

the last two lines in the buildClone() function is what I want to avoid. I would like the method to assign the new id to the specified element right in the method, rather then it just returning a new ID.

This is what I came up with

Element.prototype.theID = theID;

function theID(){
//this.makeNewID code
//code that builds a new ID and stores it in a variable called nwID
this.setAttribute('id', nwID);//asignes the ID to the element.
}

function buildClone(cloneThis){
//builds  a clone out of the specified cloneThis element and stores it in a variable
//called clonedElement
 clonedElement.theID();//determines a new ID
 }

This new way I attempted it doesn't work, I have also tried return clonedElement.theID; and it doesn't seem to work. Any idea on what I'm doing wrong?

I apologize, It was an error in my part posting it on here, but I fixed it, this is what it actually looks like and It still doesn't work.

Upvotes: 0

Views: 46

Answers (1)

Joe
Joe

Reputation: 82604

theID is a function, so it needs to be called:

function buildClone(cloneThis){
    //builds  a clone out of the specified cloneThis element and stores it in a variable
    //called clonedElement
    clonedElement.theID(); //determines a new ID
}

Upvotes: 1

Related Questions