JavaScript:The Good Parts学习笔记1
来源:互联网 发布:淘宝排名前十的店铺 编辑:程序博客网 时间:2024/04/29 16:05
扩展原型方法
//Add a method conditionally.Function.prototype.method=function(name,func){ if(!this.prototype[name]){ this.prototype[name]=func; }};
上面的代码功能类似于
Function.prototype.someMethod=Function.prototype.someMethod||function(){}可以避免粗鲁覆盖原型中已有的某些方法,而且你不需要每次都写'prototype'。
数组的一些基本方法的实现方式
1 Array.splice(start,deleteCount,item...)
/* * [--start(------deleteCount-----)(--shiftCount--)] * [--start(-insertCount-)(-delta-)(--shiftCount--)] * [--start(---------insertCount-----------)(--shiftCount--)] * (-delta-) */Array.method("mySplice", function (start, deleteCount) { start = start || 0; if (start < 0) { start += len; } start = max(min(start, len), 0); deleteCount = max(min(typeof deleteCount === "number" ? deleteCount : len, len - start), 0); var max = Math.max, min = Math.min, k = 0, len = this.length, insertCount = max(arguments.length - 2, 0), shiftCount = len - start - deleteCount, delta = insertCount - deleteCount, newLen = len + delta, result = []; for (var i = 0; i < deleteCount; i++) { if(this[start + i] !== 'undefined'){ //sparse array result[i] = this[start + i]; } } if (delta < 0) { k = start + insertCount; while (shiftCount) { this[k] = this[k - delta]; shiftCount--; k++; } } if (delta > 0) { k = 1; while (shiftCount) { this[newLen - k] = this[len - k]; shiftCount--; k++; } this.length = newLen; //update length } for (var j = 0; j < insertCount; j++) { this[start + j] = arguments[2 + j]; } return result;});
备注:JavaScript中的数组为稀疏数组,增大length在firebug中会以'undifined'扩展数组,chrome控制台中数组不变,减小length会截断数组,length大小为数组最后一个元素的索引值加1(并不总是等于元素个数);
Array.splice()返回一个包含删除元素的数组,删除添加都在原数组上进行操作(区别于Array.slice()),所以可以用于删除数组元素;
delete操作同样可以删除数组元素,但是会使数组稀疏化(后面的元素索引不变,留下一个“洞”在原数组上);
2 Array.pop()
Array.method("myPop",function(){ return this.splice(this.length - 1, 1)[0]; //splice() method returns an array});
3 Array.push()
Array.method("myPush",function(){ this.splice.apply(this, [this.length, 0].concat(Array.prototype.slice.apply(arguments))); return this.length; //push() method returns the length of the array});
4 Array.shift()
Array.method("myShift",function(){ return this.splice(0,1)[0];});
5 Array.unshift()
Array.method("myUnshift",function(){ this.splice.apply(this, [0, 0].concat(Array.prototype.slice.apply(arguments))); return this.length;});备注:pop/push/shift/unshift/方法均改变了原数组,所以用splice()方法实现;
splice()方法返回的是数组,要加上索引‘[0]’才能正确的实现pop/push等方法;
Array.prototype.slice.apply()可以将arguments类数组转化为数组,再传入concat方法;
apply的两个作用,1 对象冒充;2 传入数组参数;
REFERENCE: JavaScript花园、JavaScript精粹、JavaScript权威指南、网络资料
0 0
- JavaScript:The Good Parts学习笔记1
- JavaScript: The Good Parts
- JavaScript the Good Parts 读书笔记
- 读书笔记 JavaScript:The Good Parts
- JavaScript Good Parts学习笔记-第二章 语法。
- JavaScript Good Parts学习笔记-第三章 对象
- JavaScript The Good Parts 代码错误
- 读 《JavaScript: The Good Parts》 有感
- javascript the good parts Chapter 4 Functions
- 烂笔头:Javascript The Good Parts
- {Java The Good Parts}
- OReilly JavaScript The Good Parts 关于变量的使用
- OReilly JavaScript The Good Parts 关于变量Property的访问
- OReilly JavaScript The Good Parts 关于Object.prototype和delete
- 《JavaScript:The Good Parts(影印版)》克罗克福特
- JavaScript : the good parts(语言精髓读书笔记)
- The R Formula Method: The Good Parts
- jms学习笔记GOOD
- iOS 中的NSTimer定时器的作用
- 第11周项目4特殊三位数
- android orm持久层框架
- JAVA编码(中文转码)问题总结
- 第11周项目六 回文~素数(4)
- JavaScript:The Good Parts学习笔记1
- 一个链表引发的血案
- JS基础——数据类型
- 聚类
- 爱的药方
- Visual Studio中最常用的13个快捷键
- Failure [INSTALL_FAILED_OLDER_SDK] Android-L, Android Studio.
- 利用Git来搭建自己的博客平台
- Python脚本如何生成Windows可执行文件.exe