实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Hybrid Factory Pattern Example 2</title>
5
    <script type="text/javascript">
6
                    
7
        function SpecialArray(){       
8
 
9
            //create the array
10
            var values = new Array();
11
            
12
            //add the values
13
            values.push.apply(values, arguments);
14
            
15
            //assign the method
16
            values.toPipedString = function(){
17
                return this.join("|");
18
            };
19
            
20
            //return it
21
            return values;        
22
        }
23
        
24
        var colors = new SpecialArray("red", "blue", "green");
25
        alert(colors.toPipedString()); //"red|blue|green"
26
27
        alert(colors instanceof SpecialArray);
28
29
        
30
    </script>
31
</head>
32
<body>
33
34
</body>
35
</html>