Array.prototype中函数定义

在Ecmascript标准中我们常常看到这样的描述:

某某函数被有意设计成通用的;它的this值并非必须是数组对象。因此,它可以作为方法转移到其他类型的对象中。一个宿主对象是否可以正确应用这个splice函数是依赖于实现的。

在javascript中有两个很有用的函数callapply,他们的作用都是使用另一个对象替换当前的对象,只是在调用的参数上有少许的区别。说的更具体一点儿就是改变this指向的对象。这个功能还是还有用的,可以使用一些内置的函数来处理我们自己定义的对象(好处是效率高,而且不用自己写代码)。比如删除obj中前五个和后五个数据可以这样写[].slice.call(obj,5,-5)

我们比较感兴趣的是的当我们有一个对象,那些我们需要的功能可是使用内置的这些函数来处理以及对这个对象的要求是什么。我们需要知道这些内置对象的实现。这个就需要我们去查阅Ecmascript协议标准,可是大家都懂得,标准写的还是很有技术水平的,不是一时半会儿可以看得懂的。所以我就用大家熟悉的javascript近似的实现了Array.prototype中的函数,供大家查阅。结果发现,其实一个对象基本是只要有length属性就可以使用这个对象作为Array.prototype中函数的this执行而不出错,而且length属性不必非要是数字。只要可以转换成整型就可以。另外让大家知道一个函数的具体作用没有比贴出源码更好的方式了(虽然大家都不喜欢看),因此这个也可以作为一个学习javascript的资料。最后,有些函数在低版本的浏览器中没有实现,因此可以用这些代码扩展低版本的浏览器。难免有所疏漏,请大家予以指正。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
Array.prototype.constructor = Array;
Array.prototype.toString = function(){
    //需要的属性 join
    var array = this; // toObject 后面这样的注释都表示强制类型转换
    var func = array.join;
    // if (! func instanceof Function) {throw "type Error"}
    return func.call(array);
};
Array.prototype.concat = function (item1)
{
    var O = this;  // toObject
    var A = []; var n = 0;
    var items = [O];var i;
    for(i = 0; i < arguments.length; i++) {
        items.push(arguments[i]); // 标准里没有使用push实现这个
    }
    while(items.length){
        var E = items.shift();  //标准里写的不是shift函数实现这个
        if(E instanceof Array){
            var k = 0; var len = E.length;
            while(k < len){
                if(E.hasOwnPropery(k.toString())){
                    A[n] = E[k];
                }
                n++; k++;
            }
        }else{
            A[n] = E;
            n++;
        }
    }
    return A;
};
Array.prototype.join = function(sepatator)
{
    var O = this;  // toObject
    var lenVal = O.length;
    var len = lenVal; //toUint32
    sepatator = sepatator || ",";
    var sep = sepatator.toString();
    if(len === 0) { return ""; } 
    var element0 = O[0];
    var R = element0 === undefined || element0 === null ? "" : element0.toString();
    var k = 1;
    while(k < len){
        var S = R + sep;
        element = O[k];
        var next = element === undefined || element === null ? "" : element.toString();
        R = S + next;
        k++;
    }
    return R;
};
Array.prototype.pop = function (){
    var O = this; // toObject
    var lenVal = O.length;
    var len = lenVal; // 强制类型转换toUint32;
    if(len === 0){
        O.length = 0;
        return undefined;
    }
    var element = O[len-1];
    delete O[len-1];  //对于数组,这句可以没有,但是对于obj这是必须的
    O.length = len-1;
    return element;
};
Array.prototype.push = function(item1) {
    var O = this ; //toObject
    var lenVal = O.length;
    var n = lenVal; // toUint32;
    var items = [O];var i;
    for(i = 0; i < arguments.length; i++) {
        items.push(arguments[i]); // 标准里没有使用push实现这个
    }
    while(items.length > 0){
        var E = items.shift();
        O[n] = E;
        n++;
    }
    O.length = n;
    return n;
};
Array.prototype.reverse = function () {
    var O = this;  //toObject
    var lenVal = O.length;
    var len = lenVal; // toUint32
    var middle = Math.floor(len/2);
    var lower = 0;
    while(lower !== middle){ //不知道为什么标准里写的不是 lower >= middle
        var upper = len - lower - 1;
        var lowerValue = O[lower];
        var upperValue = O[upper];
        //这一段处理真正的体现了这个函数不止限于Array调用
        if(O.hasOwnPropery(lower.toString()) && O.hasOwnPropery(upper.toString())){
            O[lower] = upperValue;
            O[upper] = lowerValue;
        }else if(!O.hasOwnPropery(lower.toString()) && O.hasOwnPropery(upper.toString())){
            O[lower] = upperValue;
            delete O[upper] ;
        }else if( O.hasOwnPropery(lower.toString()) && !O.hasOwnPropery(upper.toString()) ){
            delete O[lower];
            O[upper] = lowerValue;
        }
        lower++;
    }
    return O;
};
Array.prototype.shift = function (){
    var O = this; //toObject
    var lenVal = O.length;
    var len = lenVal; // toUint32
    if(len === 0) { O.length = 0; return undefined; }
    var first = O[0];
    var k =1;
    while(k < len){
        var from = k.toString();
        if(O.hasOwnPropery(from)){
            O[k-1] = O[k];
        }else{
            delete O[k-1];
        }
        k++;
    }
    delete O[k-1];
    O.length = len - 1;
    return first;
};
Array.prototype.slice = function (start, end){
    var O = this; //toObject
    var A = [];
    var lenVal = O.length;
    var len = lenVal; // toUint32
    var relativeStart = start; // toIntger
    var k = relativeStart < 0 ? Math.max(len + relativeStart,0):Math.min(relativeStart,len);
    var relatveEnd = typeof end ==="undefined" ? len : end; // toIntger(end)
    var final_ = relatveEnd < 0 ? max(len+relatveEnd,0) : min(relativeEnd,len);
    var n  = 0;
    while(k < final_){
        if(O.hasOwnPropery(k.toString())){
            A[n] = O[k];
        }
        k++;n++;
    }
    return A;
};
Array.prototype.sort = function (){
};
Array.prototype.splice = function (start,deleteCount) {
    var O = this; //toObject
    var A = [];
    var lenVal = O.length;
    var len = lenVal; // toUint32
    var relatveStart = start; // toIntger
    var actualStart = relatveStart < 0 ? Math.max(len + relatveStart,0): Math.min(relatveStart,len);
    var actualDeleteCount = Math.min(max(deleteCount,0),len - actualStart); //toIntger(deleteCount)
    var k = 0;
    var from;
    while(k < actualDeleteCount){
        from = (actualStart+k).toString();
        if(O.hasOwnPropery(from)){
            A[k] = O[actualStart+k];
        }
        k++;
    }
    var items = [];
    for(i = 2; i < arguments.length; i++) {
        items.push(arguments[i]); // 标准里没有使用push实现这个
    }
    var itemCount = items.length;
    if(itemCount < actualDeleteCount){
        k = actualStart;
        while(k < len - actualDeleteCount){
            from = (k+actualDeleteCount).toString();
            if(O.hasOwnPropery(from)){
                O[k+itemCount] = O[k+actualDeleteCount];
            }else{
                delete O[k+itemsCount];
            }
            k++;
        }
        k = len;
        while(k > len-actualDeleteCount + itemsCount){
            delete O[k-1];
            k--;
        }
    }else if(itemCount > actualDeleteCount){
        k = len - actualDeleteCount;
        while(k > actualStart){
            from = (k+actualDeleteCount - 1).toString();
            if(O.hasOwnPropery(from)){
                O[k + itemCount - 1] = O[k+actualDeleteCount -1];
            }else{
                delete O[k + itemCount -1];
            }
            k--;
        }
    }
    k = actualStart;
    while(items.length>0){
        var E = items.shift();
        O[k] = E;
        k++;
    }
    O.length = len - actualDeleteCount + itemCount;
    return A;
};
Array.prototype.unshift = function (item1){
    var O = this; //toObject
    var lenVal = O.length;
    var len = lenVal; // toUint32
    var argCount = arguments.length;
    var k = len;
    while(k > 0){
        var from = (k-1).toString();
        if(O.hasOwnPropery(from)){
            O[k + argCount - 1] = O [k-1];
        }else{
            delete O[k + argCount -1];
        }
        k--;
    }
    var j = 0;
    var items = []; var i;
    for(i = 0 ; i < arguments.length;i++){
        items.push(arguments[i]);
    }
    while(items.length > 0){
        var E = items.shift();
        O[j] = E;
        j++;
    }
    O.length = len + argCount;
    return len+argCount;
};
Array.prototype.indexOf = function (searchElement){
    var O = this; //toObject
    var lenVal = O.length;
    var len = lenVal; // toUint32
    if(len === 0) { return -1; }
    var n = arguments.length >=2 ? arguments[1] : 0; //toIntger(arguments[1])
    if(n > len) { return -1; }
    var k = n>=0 ? n : Math.max(len + n, 0);
    while(k < len){
        if(O.hasOwnPropery(k.toString)()){
            if(searchElement === O[k]) {
                return k;
            }
        }
        k++;
    }
    return -1;
};
Array.prototype.lastIndexOf = function (searchElement){
    var O = this;
    var lenVal = O.length;
    var len = lenVal; // toUint32
    if(len === 0) { return -1; }
    var n = arguments.length >=2 ? arguments[1] : 0; //toIntger(arguments[1])
    var k = n>=0 ? min(n,len-1) : len + n;
    while(k >= 0){
        if(O.hasOwnPropery(k.toString)()){
            if(searchElement === O[k]) {
                return k;
            }
        }
        k++;
    }
    return -1;
};
Array.prototype.every = function (callbackfn){
    var O = this; //toObject
    var lenVal = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    var T = arguments.length >=2 ? arguments[1] : undefined;
    var k = 0;
    while(k < len){
        if(O.hasOwnPropery(k.toString())){
            var testResult = callbackfn.call(T,O[k],k,O);
            if(!testResult) { return false; }
        }
        k++;
    }
    return true;
};
Array.prototype.some = function (callbackfn){
    var O = this; //toObject
    var lenVal = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    var T = arguments.length >=2 ? arguments[1] : undefined;
    var k = 0;
    while(k < len){
        if(O.hasOwnPropery(k.toString())){
            var testResult = callbackfn.call(T,O[k],k,O);
            if(testResult) { return true; }
        }
        k++;
    }
    return false;
};
Array.prototype.forEach = function(callbackfn){
    var O = this; //toObject
    var lenValue = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    var T = arguments.length >=2 ? arguments[1] : undefined;
    var k = 0;
    while( k < len ){
        if(O.hasOwnPropery(k.toString())){
            callbackfn.call(T,O[k],k,O);
        }
        k++;
    }
    return undefined;
};
Array.prototype.map = function(callbackfn){
    var O = this; //toObject
    var lenValue = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    var T = arguments.length >=2 ? arguments[1] : undefined;
    var A = [];
    var k = 0;
    while( k < len ){
        if(O.hasOwnPropery(k.toString())){
            A[k] = callbackfn.call(T,O[k],k,O);
        }
        k++;
    }
    return A;
};
Array.prototype.filter = function(callbackfn){
    var O = this; //toObject
    var lenValue = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    var T = arguments.length >=2 ? arguments[1] : undefined;
    var A = [];
    var k = 0;
    var to = 0;
    while( k < len ){
        if(O.hasOwnPropery(k.toString()) ){
            if( callbackfn.call(T,O[k],k,O) ){
                A[to] = O[k];
                to++;
            }
        }
        k++;
    }
    return A;
};
Array.prototype.reduce = function (callbackfn){
    var O = this; //toObject
    var lenValue = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    //if(len === 0 && arguments.length <  2 ) {throw "type error"}
    var k = 0;
    var accumulator;
    if(arguments.length > 2) { 
        accumulator = arguments[1]; 
    }else{
        var kPresent = false;
        while(kPresent === false && k < len){
            kPresent = O.hasOwnPropery(k.toString());
            if(kPresent){
                accumulator = O[k];
            }
            k++;
        }
        // if(!kPresent) {throw "type error"}
    }
    while( k < len ){
        if(O.hasOwnPropery(k.toString())){
            accumulator = callbackfn.call(undefined,accumulator,O[k],k,O);
        }
        k++;
    }
    return accumulator;
};
Array.prototype.reduceRight = function (callbackfn){
    var O = this; //toObject
    var lenValue = O.length;
    var len = lenVal; // toUint32
    //if(! callbackfn instanceof Function) {throw "typeerror"}
    //if(len === 0 && arguments.length <  2 ) {throw "type error"}
    var k = len - 1;
    var accumulator;
    if(arguments.length > 2) { 
        accumulator = arguments[1]; 
    }else{
        var kPresent = false;
        while(kPresent === false && k >= 0){
            kPresent = O.hasOwnPropery(k.toString());
            if(kPresent){
                accumulator = O[k];
            }
            k--;
        }
        // if(!kPresent) {throw "type error"}
    }
    while( k >= 0 ){
        if(O.hasOwnPropery(k.toString())){
            accumulator = callbackfn.call(undefined,accumulator,O[k],k,O);
        }
        k--;
    }
    return accumulator;
};