Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C#类转换为JavaScript_C#_Javascript_Class - Fatal编程技术网

将C#类转换为JavaScript

将C#类转换为JavaScript,c#,javascript,class,C#,Javascript,Class,看看这个基本类: namespace AcmeWeb { public string FirstName { get; set; } public class Person { public Person(string firstName, string lastName) { if (String.IsNullOrEmpty(firstName)) {

看看这个基本类:

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);
然后您可以新建一个
人员

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

(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());
通过返回object literal,将关闭构造函数中的所有局部变量。这就是为什么它们是私有的,并且只能从Person对象内部访问。公共方法是那些包含在返回对象文本中的方法


示例:

您可以在javascript中使用真正的getter/setter。有关更多信息,请参见John Resig


首先,为什么要将其转换为JS?如果是因为您需要客户端上的对象,您可以将类解析为JSON并将其传递给客户端,然后你就可以像对待服务器上的类一样对待客户端上的JSON对象了。我只是在探索如何让JavaScript更面向对象。有一种叫做Script的东西,它是C到JavaScript的编译器:我从来没有尝试过,但是这个主意听起来很棒。我喜欢这个答案,因为在JS代码中,访问器/变异器除了让你不得不调用
person.firstname()
@Kirk Woll-,还有什么用呢。我只在Chrome上测试过这个。
(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());
(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