当前位置: 代码迷 >> Web前端 >> swfupload 源码翻阅笔记二
  详细解决方案

swfupload 源码翻阅笔记二

热度:764   发布时间:2012-11-04 10:42:41.0
swfupload 源码阅读笔记二

?

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/

?

1 楼 三尺寒冰 2011-07-20  
深刻呀。。。。