2014-03-25 31 views
3

我在Node.js中編寫面向對象的Cat類時遇到了很多麻煩。我怎麼能寫一個Cat.js類,並以下列方式使用它:如何編寫面向對象的Node.js模型

// following 10 lines of code is in another file "app.js" that is outside 
// the folder "model" 
var Cat = require('./model/Cat.js'); 

var cat1 = new Cat(12, 'Tom'); 
cat1.setAge(100); 
console.log(cat1.getAge()); // prints out 100 to console 

var cat2 = new Cat(100, 'Jerry'); 
console.log(cat1.equals(cat2)); // prints out false 

var sameAsCat1 = new Cat(100, 'Tom'); 
console.log(cat1.equals(sameAsCat1)); // prints out True 

你會如何解決我寫了下面的Cat.js類:

var Cat = function() { 
    this.fields = { 
     age: null, 
     name: null 
    }; 

    this.fill = function (newFields) { 
     for(var field in this.fields) { 
      if(this.fields[field] !== 'undefined') { 
       this.fields[field] = newFields[field]; 
      } 
     } 
    }; 

    this.getAge = function() { 
     return this.fields['age']; 
    }; 

    this.getName = function() { 
     return this.fields['name']; 
    }; 

    this.setAge = function(newAge) { 
     this.fields['age'] = newAge; 
    }; 

    this.equals = function(otherCat) { 
     if (this.fields['age'] === otherCat.getAge() && 
      this.fields['name'] === otherCat.getName()) { 
      return true; 
     } else { 
      return false; 
     } 
    }; 
}; 

module.exports = function(newFields) { 
    var instance = new Cat(); 
    instance.fill(newFields); 
    return instance; 
}; 
+0

這是否是唯一的問題,我不能說,但是你想在'fill'函數中測試'typeof()!=='undefined''。而且因爲你給它們分配了'null'值,所以無論如何不會出現這種情況,因爲你會在'typeof'上返回'object'。如果你只是把它們賦值爲'undefined'並且通過'typeof()'來測試它,否則它會出現它應該像你期望的那樣行爲。 –

+1

到底需要幫助的問題究竟是什麼? – jfriend00

+0

你可能想看看[TidBits OoJs](https://github.com/najamelan/TidBits_Javascript_OoJs)。它是可靠的,並給你關於你可能夢寐以求的所有OO功能,除了以簡單和自然的語法進行多重繼承。 – nus

回答

3

如果我來設計這樣一個對象,然後我會喜歡這個

function Cat(age, name) {  // Accept name and age in the constructor 
    this.name = name || null; 
    this.age = age || null; 
} 

Cat.prototype.getAge = function() { 
    return this.age; 
} 

Cat.prototype.setAge = function(age) { 
    this.age = age; 
} 

Cat.prototype.getName = function() { 
    return this.name; 
} 

Cat.prototype.setName = function(name) { 
    this.name = name; 
} 

Cat.prototype.equals = function(otherCat) { 
    return otherCat.getName() == this.getName() 
     && otherCat.getAge() == this.getAge(); 
} 

Cat.prototype.fill = function(newFields) { 
    for (var field in newFields) { 
     if (this.hasOwnProperty(field) && newFields.hasOwnProperty(field)) { 
      if (this[field] !== 'undefined') { 
       this[field] = newFields[field]; 
      } 
     } 
    } 
}; 

module.exports = Cat;  // Export the Cat function as it is 

然後它可以像這樣使用

var Cat = require("./Cat.js"); 

var cat1 = new Cat(12, 'Tom'); 
cat1.setAge(100); 
console.log(cat1.getAge());     // 100 

var cat2 = new Cat(100, 'Jerry'); 
console.log(cat1.equals(cat2));    // false 

var sameAsCat1 = new Cat(100, 'Tom'); 
console.log(cat1.equals(sameAsCat1));  // true 

var sameAsCat2 = new Cat(); 
console.log(cat2.equals(sameAsCat2));  // false 

sameAsCat2.fill({name: "Jerry", age: 100}); 
console.log(cat2.equals(sameAsCat2));  // true 
+0

你知道爲什麼equals方法不起作用嗎? –

+0

@Chingy我假設你的。試用'=='而不是'===':) – thefourtheye

-4

我會用打字稿:

class Cat{ 
    fields = { 
     age: null, 
     name: null 
    }; 

    fill(newFields) { 
     for(var field in this.fields) { 
      if(this.fields[field] !== 'undefined') { 
       this.fields[field] = newFields[field]; 
      } 
     } 
    } 

    getAge() { 
     return this.fields.age; 
    } 

    setAge(newAge:number) { 
     this.fields.age = newAge; 
    } 
} 

export = Cat; 

You can see the javascript it generates.

打字稿可在兩個和的NodeJS瀏覽器一起使用。

0

此代碼是在這裏工作精細Person.js代碼

exports.person=function(age,name) 
    { 
     age=age; 
     name=name; 
     this.setAge=function(agedata) 
     { 
      age=agedata; 
     } 
     this.getAge=function() 
     { 
      return age; 
     } 
     this.setName=function(name) 
     { 
      name=name; 
     } 
     this.getName=function() 
     { 
      return name; 
     } 

}; 

調用對象代碼:

var test=require('./route/person.js'); 
var person=test.person; 
var data=new person(12,'murugan'); 
data.setAge(13); 
console.log(data.getAge()); 
data.setName('murugan'); 
console.log(data.getName());