簡體   English   中英

我如何從此函數構造函數獲取全名

[英]How do i get the full name from this function constructor

我正在嘗試(並且慘遭失敗)使用我的函數構造函數中的函數來檢索manTwo的全名。 任何幫助將不勝感激。

function clenk(arg1, arg2) {
this.firstName = arg1;
this.lastName  = arg2;
fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}


var manTwo = new clenk("Mike","Tool");
manTwo.fullName();

更改

fullName: function () {

this.fullName = function () {

您還可以使用原型:

function clenk(arg1, arg2) {
    this.firstName = arg1;
    this.lastName  = arg2;
}
clenk.prototype.fullName = function () {
    return this.firstName + " " + this.lastName;
}

如果您創建了許多clenk實例,這會更有效率(請注意,標准做法是對“類”的首字母使用大寫字母)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM