我被困在Codewars.com的Javascript挑戰中。它看起來是一個構造函數(?)函數,我認爲我知道,但唉,不。 我在MDN和其他地方尋找例子。 我寧願知道錯誤的觀點而不是直截了當的答案,看看我能否認出我自己。 挑戰:Codewars構造函數
function new Person(name){
this.name = name;
return name;
}
Person.prototype.greet = function(otherName){
return "Hi " + otherName + ", my name is " + name;
};
通過做一些研究,我想也許增加了構造的兩個名字會做,但再次得到一個「意外的標記新」的錯誤。我嘗試:
function new Person(name, otherName){
this.name = name;
this.otherName = otherName;
return name + " " + otherName;
}
Person.prototype.greet = function(otherName){
return "Hi " + otherName + ", my name is " + name;
};
var fullName = new Person("Fred", "Jones");
greet.fullName();
耐心,我的無知,是極大的讚賞。 謝謝, pychap
請仔細閱讀[問]。這裏沒有一個適當的問題陳述,也沒有一個具體的問題 – charlietfl
只需開始在* javascript工廠模式*上搜索,嘗試一下,然後回來一個更加一致的例子。那麼我們很樂意爲您提供幫助! – pietro909