Person {     public
FirstName { get; set; }     public
LastName { get; set; }     public
GetFullName()     {         return
FirstName + "
+ LastName;     } } 

当你new 一个时候,比如:p = new Person;


如果你通过 p1 来访问它的method,其实是先找到Person的实例,从它的内存列表里找到method,然后
从引用列表里,找到P1,然后返回P1实例内存里的数据。这些在DotNet里都是封装在CLR(Common Language Runtime,DotNet Framework)里的。
这里顺便提一下 C++中的对象模型,可以参考下面的link:
在Javascript 中,我们如果想做到上面这种效果,可以按如下的写法:【熟悉JS的一看就明白了】,
Person(fn,ln) {     this.FirstName = fn;     this.LastName = ln;     if(typeof
this.GetFullName != "function"){         Person.prototype.GetFullName = function()         {             return
this.FirstName + '
+ this.LastName;         }     } } var
p = new
Person('jon','smith'); alert(p.GetFullName()); 

那如何把JS封装的好一点呢? 如果你用过C#的话,都会觉得C#的namespace封装的很好,当然以前Delphi也很好,因为他们是
同一个作者搞的。所以在JS里如果能用 like, 这样的对象是一件很开心的事情,尤其在大型web application里。

Namespace = {
        Register : function(ns) {
            //Split the string
            var nsParts=ns.split(“.”);
            //Store window in obj
            var obj=window;
            //Travese through the string parts
            for(var i=0, j=nsParts.length; i<j; i++)    {
                if(typeof obj[nsParts[i]] == “undefined”) {
                    //If it did not exist create it and copy it to obj
                    //So the next one can be created in this one
                else    {
                    //Still copy it to obj, cause the next one might not exist
window.System.IO. 截图如下:

理解了这个,应该很能理解五花八门的JS OOP封装!或者可以看看TypeScript的source code,因为它是c#,delphi同一个作者发明的。
For reading some ugly code from the company project and 有感而写和继续学习。


This entry was posted in 技术开发 and tagged . Bookmark the permalink.

8 Responses to JavaScript和C#以及OOP笔记

  1. Madhumita says:

    Thanks for inicndurtog a little rationality into this debate.

  2. I read your post and wished I’d written it

  3. impotence says:

    Essays like this are so important to broadening people’s horizons.

  4. Living in Canada so would love to enter, originally from Scotland and back there my local supermarket is Tesco!! Love shopping back home so many different and tasty things!!

  5. I have loaded your blog in Several totally different web browsers and I must say your blog loads a lot quicker then most. Would you mind e-mailing me the company name of your hosting company? My personal e-mail is: . I’ll even sign up through your affiliate link if you would like. Thanks a lot

  6. I found a great new e-book that answered alot of my questions on this subject. Very easy to read and worth the time to download it! There is also a great mini-ecourse that gave me a few ideas I never thought of before!

    • Ricky says:

      I don't believe it. You tube what have you done! You have taken away a great feature. When someone reponds to a comment that i posted this would make the relevent posting title in my "my ac;ount&quotc-"video comments" section display in bold text. This would let you know someone was responding to your comment. But now they are all in bold! The feature is useless now. What a dissaster. This was the feature i used most! Daft.

  7. Hærlig:) Så godt å høre at frøkna er ute av vinterdepresjonen sin:) Tihi,kan se dere for meg Kira foran og du krypende etter for å få et perfekt bilde:) Og det er verdt det:) Tror ikke du hadde gitt opp fotagraferinga om møblene hadde rørt seg heller:) Deilig å ikke jobbe nå når været er så fantastisk ikke sant?! Nyt det MArit:) Klem Eva