2011-09-19 10 views
0

저는 사용자 지정 개체를 처음 사용하지만 특히 장기간에 걸쳐 많은 코드 작성을 줄이기 때문에 매우 유용하다는 사실을 알았습니다.개체 속성을 변경하는 사용자 지정 메서드 만들기

복제 된 요소의 일부 속성을 기반으로 새로운 고유 ID를 만드는 메서드를 사용하는 특정 알고리즘을 사용하고 있습니다. 이것은 바로 지금 보이는 것입니다 :

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. 
} 

buildClone() 함수의 마지막 두 줄은 피하고 싶습니다. 메서드에서 지정된 요소에 새 ID를 할당하는 대신 메서드에서 새 ID를 반환하는 것이 좋습니다. 이것은 내가

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 
} 

나는 그것이 작동하지 않는 시도이 새로운 방법을 해낸 것입니다

, 나는 또한 return clonedElement.theID;을 시도하고 작동하지 않는 것. 내가 뭘 잘못하고 있는거야?

죄송합니다. 여기에 게시하는 것이 잘못되었지만 실제로 고쳐졌습니다. 실제로는 보이지만 여전히 작동하지 않습니다.

+0

는'VAR NWID = clonedElement.theID는 안된다',''var에 NWID = clonedElement.theID()를 할 수? – epascarello

+0

죄송합니다. 고정되었지만 괄호로는 여전히 작동하지 않습니다. –

답변

1

theID 함수이므로 호출 할 필요가있다 :

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 
} 
+0

죄송합니다. 매개 변수 영역이 있지만 작동하지 않았습니다. –

관련 문제