function WArray() { this.value = []; // åå§å wArray对象 æ¶ï¼éé¢å®ä¹ ä¸ä¸ªç©ºæ°ç»ï¼å°æ¥ç¨æ¥è£ æ°æ®çå®¹å¨ this.length = 0; //å 为æ¯ç©ºæ°ç»ï¼æä»¥ç°å¨çé¿åº¦æ¯0 /** * pushï¼ï¼æ¹æ³ç¨äºå¨æ°ç»çæ«ç«¯æ·»å æ°ç»çå ç´ ï¼å¹¶è¿åæ·»å æ°å ç´ åçæ°ç»é¿åº¦ã * å¦æå ¥åä¸åæ³ï¼å°ä¼æé; * 妿è¿è¡åºéï¼å°ä¼è¿å空æ°ç»ã * @param {...any} item1 * @returns */ this.push = function (...item1) { // å½ä¼ å ¥å¤ä¸ªåæ°å ¥æ¥æ¶ å¯ç¨ ...å½¢åå 表示 if (item1 == undefined || item1 == null || item1.length == 0) { throw new error('å ¥åä¸åæ³'); } for (let i = this.value.length, j = 0; j < item1.length; i++, j++) { this.value[i] = item1[j] } this.length += item1.length; return this.length; }; this.map0 = function (callback) { //1ã夿 this.valueä¸ææ²¡æå ç´ ï¼ if (this.value.length == 0) { throw new Error('å 鍿 æ°æ®'); } let mapArr2 = []; for (let i = 0; i < this.value.length; i++) { let mapArr1 = []; mapArr1 += this.value[i]; mapArr2 += mapArr1; mapArr2++; } return mapArr2; }; this.map1 = function (callback) { if (typeof callback != 'function') { throw new Error('callback is not a function!'); } let mapArr2 = new WArray(); for (let i = 0; i < this.value.length; i++) { //mapArr2[i] = callback(this.value[i]); mapArr2.push(callback(this.value[i])); } return mapArr2; }; /** * mapï¼ï¼æ¹æ³ å°æ°ç»ç æææå 便¬¡ ä¼ å ¥åæ°ï¼ * ç¶åææ¯ä¸æ¬¡çæ§è¡ç»æ ç»æä¸ä¸ªæ°çæ°ç»è¿å,åæ°ç»æ²¡æååã * @returns mapArr */ this.map2 = function (callback) { if (typeof callback != 'function') { throw new Error('callback is not a function!'); } let mapArr2 = []; for (let i = 0; i < this.value.length; i++) { // mapArr2[i] = callback(this.value[i]); mapArr2.push(callback(this.value[i])); } return mapArr2; } } let wArray = new WArray(); wArray.push(1, 2, 3, 4,); console.log(`before: `, wArray); let callbackWei = function (x) { return x + 1; } let mapArr = wArray.map2(callbackWei); console.log(`after: `, wArray); console.log(`after: mapArr = `, mapArr);