我有以下代码:从函数内部调用函数?
var A = function (id) {
var elem = document.getElementById(id);
function text() {
return "Hello world";
}
this.other = function(){
console.log(text());
}
}
如果我想从外面添加other
功能,还自称喜欢这个文本()函数:
(function(A){
A.prototype.other = function() {
console.log(text());
}
})(A);
有没有办法做到那?我的意思是,无需更改function text(){}
到this.text=function(){}
** **为什么你不希望改变'功能文本(){}''到= this.text功能(){}'? – phihag
因为我不希望它被覆盖。 'var a = new A(); a.text = function(){}' – IchHabsDrauf
然后你的问题应该是'我如何防止名称被覆盖',并提及为什么[属性](https://developer.mozilla.org/en/docs/ Web/JavaScript/Reference/Global_Objects/Object/defineProperty)是不够的。您也应该考虑是否要包含随机覆盖其他库名称的代码。 – phihag