Javascript 创建更改对象属性的自定义方法
我不熟悉自定义对象,但发现它们非常有用,特别是因为从长远来看,自定义对象可以减少大量代码编写 我正在使用一种特定的算法来创建一个克隆元素,并使用一种基于克隆元素的某些属性创建一个新的唯一ID的方法。这就是它现在的样子:Javascript 创建更改对象属性的自定义方法,javascript,methods,properties,Javascript,Methods,Properties,我不熟悉自定义对象,但发现它们非常有用,特别是因为从长远来看,自定义对象可以减少大量代码编写 我正在使用一种特定的算法来创建一个克隆元素,并使用一种基于克隆元素的某些属性创建一个新的唯一ID的方法。这就是它现在的样子: Element.prototype.theID = theID; function theID(){ //this.makeNewID code //code that builds a new ID and stores it in a variable called nwI
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
}
我尝试的这种新方法不起作用,我也尝试了返回clonedElement.theID代码>并且它似乎不起作用。知道我做错了什么吗
我很抱歉,这是我在这里发布的一个错误,但我已经修复了它,这是它的实际外观,它仍然无法工作。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
}
不应var nwID=clonedElement.theID
bevar nwID=clonedElement.theID()代码>?很抱歉,我已经修复了它,但它仍然无法使用括号。很抱歉,它确实有参数区域,但仍然无法使用。