JavaScript和C#以及OOP笔记

【Elliot】本文来自网络,知识记录了一些心得,和大家共享。

C#中,如果你有一个object,比如 

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

当你new 一个时候,比如:p = new Person;
在内存中其实有一个p,还有一个对象我们可以称为内部对象Person。
p有自己的成员,Person有method,维护一个引用列表,什么意思呢?
举个例子,如果有p1,p2两个实例,那么内存中应该有下图这样的实例:
但不是每个实例都有method:GetFullName()。

 其实内存里维护了一个Person的实例。它有method的列表,里面维护了对每个对象的引用,如下图:




如果你通过 p1 来访问它的method,其实是先找到Person的实例,从它的内存列表里找到method,然后
从引用列表里,找到P1,然后返回P1实例内存里的数据。这些在DotNet里都是封装在CLR(Common Language Runtime,DotNet Framework)里的。
这样的话,如果有100个Person的例子,只会创建一份GetFullName的method。
这里顺便提一下 C++中的对象模型,可以参考下面的link:
上面这个link总结的很到位。这里不再赘述。
在Javascript 中,我们如果想做到上面这种效果,可以按如下的写法:【熟悉JS的一看就明白了】,
就是如果prototype里没有定义这个function,就把function放到object的prototype里,保证只创建了一份GetFullName的function。
function 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,System.io.file 这样的对象是一件很开心的事情,尤其在大型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
                    obj=obj[nsParts[i]]={};
                }
                else    {
                    //Still copy it to obj, cause the next one might not exist
                    obj=obj[nsParts[i]];
                }
            }
        }
    }
把这个代码放到Chrome的console里执行一下的话,你就明白了。然后你可以:Namespace.Register(“System.IO”);其实就是
window.System.IO. 截图如下:

理解了这个,应该很能理解五花八门的JS OOP封装!或者可以看看TypeScript的source code,因为它是c#,delphi同一个作者发明的。
Enjoy!
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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>