欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

swfupload 源码阅读笔记二

程序员文章站 2022-07-12 11:47:58
...

 

SWFUpload.prototype.initSWFUpload = function (settings) {
        try {
                this.customSettings = {};       // A container where developers can place their own settings associated with this instance.
                this.settings = settings;
                this.eventQueue = [];
                this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
                this.movieElement = null;
        // 略

 

注意:

SWFUpload.movieCount 是在 SWFUpload.prototype.initSWFUpload 语句之后定义的,

SWFUpload.movieCount = 0;

 

也就是说,要么,prototype.initSWFUpload 只是注册声明的作用,并没有被执行;要么,静态成员的初始化要先于对象成员的初始化。

 

做一个实验:

<html>
        <head>
                <script type="text/javascript">
                        //var a = 2;
                        var a;
                        if (a === undefined) {
                                alert("a is undefined.");
                                a = function(){this.initA();};
                                alert(a);
                        } else {
                                alert("a is defined.");
                        }
                
                        a.prototype.initA = function () {
                                this.count = ++a.start;
                                alert("initA: a = " + this.count); 
                        };
                        alert("before assignment of a.start");
                        a.start = 0;
                        var b = new a();
                        
                </script>
        </head>
        <body>
                
        </body>
</html>
 

执行结果是:

a is undefined.

function () {
    this.initA();
}

before assignment of a.start

initA: a = 1

 

这个只是验证了 “prototype.initSWFUpload 只是注册声明的作用,并没有被执行”。同时说明 a = function(){this.initA();}; 只是定义了 a 这个类,而不是真正的初始化;只有 var b = new a(); 才是真正的初始化。

 

将 a.start = 0; 替换到 var b = new a(); 后面,则显示

initA: a = NaN


所以静态成员必须人为地放到相关的 public 对象的初始化之前。

 

// ---------------------------------------

补充参考,关于javascript 类定义中,private, privileged, public, static member 的实现:

http://robertnyman.com/2008/10/14/javascript-how-to-get-private-privileged-public-and-static-members-properties-and-methods/