看看这个基础类:
namespace AcmeWeb
{
public string FirstName { get; set; }
public class Person
{
public Person(string firstName, string lastName)
{
if (String.IsNullOrEmpty(firstName))
{
throw new ArgumentNullException(firstName);
}
this.FirstName = firstName;
}
}
}
这段内容需要翻译成JavaScript语言,以下是我认为最佳的翻译方式:
我的思路如下:
(function(namespace) {
namespace.Person = function(firstName, lastName) {
// Constructor
(function() {
if (!firstName) {
throw "'firstName' argument cannot be null or empty";
}
})();
// Private memberts
var _ = {
firstName: firstName
};
// Public members
this.firstName = function(value) {
if (typeof(value) === "undefined") {
return _.firstName;
}
else {
_.firstName = value;
return this;
}
};
};
})(AcmeWeb);