javascript代碼如下:var A = function() { function Person(name) { this.name = name;
}
var m = function() { return "Hello " + this.name; //return "Hello ";
};
Person.prototype.getGreeting = m;
return Person;
};console.dir(A);//代碼1//結(jié)果正常,無報(bào)錯(cuò)//var B=A();//
console.log(new B("AAAA").getGreeting());
//代碼2//報(bào)錯(cuò):1.js:43 Uncaught TypeError: (intermediate value).getGreeting is not a function
console.log(new A().getGreeting());
//代碼3//結(jié)果正常,無報(bào)錯(cuò)
console.log(new(new A())("AAAA").getGreeting());
//代碼4//結(jié)果正常,無報(bào)錯(cuò)
console.log(new(A())("AAAA").getGreeting());調(diào)試過程中,發(fā)現(xiàn)報(bào)錯(cuò)部分(即代碼2)中new A()對(duì)象的原型prototype其實(shí)是包含有g(shù)etGreeting這個(gè)屬性值的。對(duì)以上代碼有如下幾個(gè)疑問:1.new A()生成的是什么?希望可以從深層次解答。2.new A()生成的對(duì)象和new(new A())("AAAA")生成對(duì)象的區(qū)別?3.代碼1和代碼3可看出new A()和A()貌似沒有什么區(qū)別,為什么?4.問題3new A()和A()如果確實(shí)是沒區(qū)別的話,那就是new關(guān)鍵字沒有起實(shí)例化作用,這該怎么理解?
訪問原型對(duì)象的函數(shù)屬性時(shí),報(bào)該屬性‘is not a function’的錯(cuò)誤?是new關(guān)鍵字
慕雪6442864
2018-10-02 13:10:47