Agi*_*ble 6 javascript c# class
看看这个基础课:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
什么是最好的翻译成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);
Run Code Online (Sandbox Code Playgroud)
您可以在 javascript 中使用真正的 getter/setter。请参阅 John Resig 的帖子了解更多信息。见小提琴。
(function(NS) {
NS.Person = function(firstName, lastName) {
if (!firstName) {
throw "'firstName' argument cannot be null or empty";
}
var FirstName = firstName;
this.__defineGetter__("FirstName", function(){
console.log('FirstName getter says ' + FirstName);
return FirstName;
});
this.__defineSetter__("FirstName", function(val){
console.log('FirstName setter says ' + val);
FirstName = val;
});
}
})(AcmeWeb);
var p = new AcmeWeb.Person('John', 'Smith');
p.FirstName; // => FirstName getter says John
p.FirstName = 'Joe'; // => FirstName setter says Joe
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5849 次 |
| 最近记录: |