js学习过程中遇到个奇怪的问题,为什么使用this.name就会出现错误提示,但是this[name]就是正常的?
报错提示为:
Uncaught TypeError: methods.fun2 is not a function
<script>
Function.prototype.addMethod = function(name,fn){
this[name] = fn;//这里用this.name就不行,为什么?
};
var methods = new Function();//没有{}
methods.addMethod('fun2',function(){
console.log("绿色污染");
});
methods.fun2();
</script>
能详细解说下何时使用this.name何时使用this[name]吗?谢谢大圣