我是新来的自定义对象,但发现它们非常有用,特别是因为从长远来看减少了大量的代码编写。制作更改对象属性的自定义方法
我正在使用特定算法创建一个克隆元素,并使用一种方法根据克隆元素的某些属性创建新的唯一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;
,它似乎并没有工作。任何想法我做错了什么?
我很抱歉,这是我在这里发布的一个错误,但我修复了它,这是它实际上的样子,它仍然不起作用。
不应'变种NWID = clonedElement.theID;'是'变种NWID = clonedElement.theID();'? – epascarello
对不起,我修正了它,但它仍然不支持括号。 –