简体   繁体   English

将C#类转换为JavaScript

[英]Converting C# class to JavaScript

Take a look at this basic 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;
        }
    }
}

What's the best translation of this into JavaScript? 什么是最好的翻译成JavaScript?

This is what I'm thinking: 这就是我的想法:

(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);

You could use real getters/setters in javascript. 你可以在javascript中使用真正的getter / setter。 See John Resig's post for more information. 有关更多信息,请参阅John Resig的帖子 See the fiddle . 看小提琴

(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
var AcmeWeb = {
    Person: function(firstName, lastName) {
        if (!firstName) {
            throw "'firstName' argument cannot be null or empty";
        }
        this.FirstName = firstName;
    }
};

Then you can new up a Person : 然后你可以新建一个Person

var person = new AcmeWeb.Person("john", "smith");

It should be like 应该是这样的

(function(namespace) {
    namespace.Person = function(firstName, lastName) {
        var firstName    = firstName || 'default',
            lastName     = lastName || 'default',
            moarPrivates = 'foo';

        return {
            firstname: function(value) {
                if( value ) {
                    firstName = value;
                    return this;
                }
                else {
                    return firstName;
                }
            },
            lastname: function(value) {
                if( value ) {
                    lastName = value;
                    return this;
                }
                else {
                    return lastName;
                }
            }
        };
    };
}(AcmeWeb));

var Andy = AcmeWeb.Person('Andy', 'Foo');

Andy.firstname('Andreas').lastname('Baaaaar');
console.log('Hello, my name is ', Andy.firstname(), ' ', Andy.lastname());

By returning on object literal, all local variables from the constructor function are closured. 通过返回对象文字,构造函数中的所有局部变量都被关闭。 That is why they are private and only accessible from within the Person object. 这就是为什么它们是私有的,只能从Person对象中访问。 The public methods are those which you enclose into the returning object literal. 公共方法是您包含在返回的对象文字中的方法。

Example: http://www.jsfiddle.net/GkFu4/1/ 示例: http//www.jsfiddle.net/GkFu4/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM