問(wèn)題一:一個(gè)方法封裝在一個(gè)對(duì)象中時(shí),直接封裝和通過(guò)prototype封裝兩種方法有什么區(qū)別?var Apple = {};
Apple.prototype.size = function size() { // do something};
Apple.size = function size() { // do something};var app1 = new Apple();var app2 = new Apple();關(guān)于prototype,我的印象是好像和原型的動(dòng)態(tài)性、prototype的方法時(shí)公用的什么的有關(guān)系,一旦app1的size改動(dòng)會(huì)影響到app2的size。但是在我目前接觸的項(xiàng)目中,大概是這樣的var City = function city(node) { this.node = document.querySelector(node); this.size = null;
……
…… this.location = {};
};
City.prototype.a = function a() {};
City.prototype.b = function b() {};
City.prototype.c = function c() {};
City.x = function x() { // do something}問(wèn)題二:這里不涉及對(duì)實(shí)例的改動(dòng),但是為什么最后的x方法不用原型而要直接這樣寫(xiě)(哈哈,這種寫(xiě)法叫什么?賦值?)呢?
方法直接封裝在對(duì)象中和通過(guò)prototype封裝有什么區(qū)別
森林海
2018-08-13 14:09:30