浅谈 ECMAScript 和 JavaScript
2016-05-27 22:17
761 查看
ES5与ES3基本保持兼容,较大的语法修正和新功能加入,将由JavaScript.next完成。
什么是ECMAScript?
http://baike.baidu.com/link?url=G1T8nGWaC0r3o-TDiDXZhgt75zEHYrG6TLxRfFjJvxpxNZHgy0Hk1Dz0RSsymSl-25oE0uUba81B7JSBc5Cw0a
ECMAScript 5.1
浏览器支持
Opera 11.60
Internet Explorer 9+
Firefox 4
Safari 5.1+
Chrome 13
"use strict" 严格模式(可靠,安全),字符串会被旧版浏览器忽略,放心使用
添加到Object上的构造器
Object.getPrototypeOf
Object.getOwnPropertyDescriptor
Object.getOwnPropertyNames
Object.create
Object.defineProperty
Object.defineProperties
Object.seal
Object.freeze
Object.preventExtensions
Object.isSealed
Object.isFrozen
Object.isExtensible
Object.keys
对象的属性:可以枚举、删除、修改
Array扩展:
Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.every
Array.prototype.some
Array.prototype.forEach
Array.prototype.map
Array.prototype.filter
Array.prototype.reduce
Array.prototype.reduceRight
代码:
typeof 运算符判断原始类型
instanceof 运算符要求程序员明确地了解变量的数据类型,比如:
原始类型:Undefined, Null, Number, Boolean, String
类型转换:
ECMAScript规定所有的对象都具有 toString()方法。
parseInt()、parseFloat()
强制类型转换:
Boolean()
Number()
String()
Boolean(""); //false - 空字符串
Boolean(null); //false - null
Boolean(0); //false - 零
强制转换成字符串和调用 toString() 方法的唯一不同之处在于,对 null 和 undefined 值强制类型转换可以生成字符串而不引发错误:
var s1 = String(null); //"null"
var oNull = null;
var s2 = oNull.toString(); //会引发错误
引用类型:
var o = new Object();
1. Object 对象 (自身用处不大)
2. Boolean 对象(很少使用,不易理解,尽量避免使用)
3. Number 对象(只要可能,都使用数字的原始表示法。尽量避免使用)
new Number(68).toFixed(2); // 68.00, 返回具有指定位数小数的数字的字符串表示
4. String 对象
length 属性
charAt()、charCodeAt()访问字符串索引的单个字符
charAt() 返回单个字符
charCodeAt() 返回单个字符的字符代码
concat() 把一个或多个字符串连接到String对象的原始值上,
一般都是用"+"连接,很少用concat()
indexOf()、lastIndexOf() 返回指定字符串的位置
indexOf() 从字符串的开头开始检索字符串
lastIndexOf() 从字符串的结尾检索字符串
slice()、substring() 都是返回要处理的字符串子串
slice(startIndex[, endIndex])
substring(startIndex[, endIndex])
对于负数参数,slice() 方法会从后往前数,substring() 方法则将其作为 0 处理(也就是说将忽略它)。
与 concat() 方法一样,slice() 和 substring() 方法都不改变 String 对象自身的值。它们只返回原始的 String 值,保持 String 对象不变 。
toLowerCase()、toLocaleLowerCase()、toUpperCase() 和 toLocaleUpperCase()
toLocaleLowerCase()、toLocaleUpperCase()更安全
代码:
ECMAScript 的核心: 函数
return 语句后面的代码都不会执行
注释:如果函数无明确的返回值,或调用了没有参数的 return 语句,那么它真正返回的值是 undefined。
arguments 对象
1. 无需明确命名参数,也可以传入参数
2. 检测参数个数
3. 模拟函数重载
ECMAScript 不会验证传入的参数和定义的参数量是否相等
ECMAScript 的函数实际上是功能完整的对象
Function 对象的length属性声明了函数期望的参数个数
Function 对象的toString()方法可以打印出函数的文本,进行调试
闭包,是指函数可以使用函数之外的变量
使用闭包要小心,因为它们可能会变得非常复杂。
代码:
什么是ECMAScript?
http://baike.baidu.com/link?url=G1T8nGWaC0r3o-TDiDXZhgt75zEHYrG6TLxRfFjJvxpxNZHgy0Hk1Dz0RSsymSl-25oE0uUba81B7JSBc5Cw0a
ECMAScript 5.1
浏览器支持
Opera 11.60
Internet Explorer 9+
Firefox 4
Safari 5.1+
Chrome 13
"use strict" 严格模式(可靠,安全),字符串会被旧版浏览器忽略,放心使用
添加到Object上的构造器
Object.getPrototypeOf
Object.getOwnPropertyDescriptor
Object.getOwnPropertyNames
Object.create
Object.defineProperty
Object.defineProperties
Object.seal
Object.freeze
Object.preventExtensions
Object.isSealed
Object.isFrozen
Object.isExtensible
Object.keys
对象的属性:可以枚举、删除、修改
Array扩展:
Array.prototype.indexOf
Array.prototype.lastIndexOf
Array.prototype.every
Array.prototype.some
Array.prototype.forEach
Array.prototype.map
Array.prototype.filter
Array.prototype.reduce
Array.prototype.reduceRight
代码:
'use strict'; /** * 全局JSON对象 * 序列化JSON.stringify ECMAScript值->JSON * 反序列化JSON.parse JSON->ECMAScript值 * * JSON.parse(text[, reviver) * JSON.stringify(value[, replacer[, space) */ var result = JSON.parse('{"a": 1, "b": 2}'); console.log(result); console.log(result.b); var result = JSON.parse('{"a": 1, "b": 2}', function (key,value) { if(typeof value == 'string') { console.log('string'); return parseInt(value); }else { console.log('other'); return value; } }); console.log(result.b); var nums = { "first": 7, "second": 14, "third": 13 }; /** * 添加replacer过滤函数操作 * space代表缩进空格 */ var luckyNums = JSON.stringify(nums, function(key, value){ if (value == 13) { return undefined; } else { return value; } }, 2); console.log(luckyNums); var cat = {}; Object.defineProperty(cat, 'name', { value: 'Maru', writable: false, enumerable: true, configurable: false }); Object.defineProperty(cat, 'skill', { value: 'exploring boxes', writable: true, enumerable: true, configurable: false }); for(var key in cat) { console.log(key + ': ' + cat[key]); } console.log(Array.isArray('No u')); console.log(Array.isArray(['No', 'u'])); var mike = JSON.stringify({mike: 'taylor'}); console.log(mike); //{"mike":"taylor"} console.log(typeof mike); //string /** * Array.isArray()直接写在了构造器上,而不是prototype对象上 */ /** * Function.prototype.bind(thisArg[, arg1[, arg2,...) */ function locate() { console.log(this.location); } function Maru(location) { this.location = location; } var maru = new Maru('some words'); var locateMaru = locate.bind(maru); locateMaru();
typeof 运算符判断原始类型
instanceof 运算符要求程序员明确地了解变量的数据类型,比如:
console.log(oStringObject instanceof String);
原始类型:Undefined, Null, Number, Boolean, String
类型转换:
ECMAScript规定所有的对象都具有 toString()方法。
parseInt()、parseFloat()
强制类型转换:
Boolean()
Number()
String()
Boolean(""); //false - 空字符串
Boolean(null); //false - null
Boolean(0); //false - 零
强制转换成字符串和调用 toString() 方法的唯一不同之处在于,对 null 和 undefined 值强制类型转换可以生成字符串而不引发错误:
var s1 = String(null); //"null"
var oNull = null;
var s2 = oNull.toString(); //会引发错误
引用类型:
var o = new Object();
1. Object 对象 (自身用处不大)
2. Boolean 对象(很少使用,不易理解,尽量避免使用)
3. Number 对象(只要可能,都使用数字的原始表示法。尽量避免使用)
new Number(68).toFixed(2); // 68.00, 返回具有指定位数小数的数字的字符串表示
4. String 对象
length 属性
charAt()、charCodeAt()访问字符串索引的单个字符
charAt() 返回单个字符
charCodeAt() 返回单个字符的字符代码
concat() 把一个或多个字符串连接到String对象的原始值上,
一般都是用"+"连接,很少用concat()
indexOf()、lastIndexOf() 返回指定字符串的位置
indexOf() 从字符串的开头开始检索字符串
lastIndexOf() 从字符串的结尾检索字符串
slice()、substring() 都是返回要处理的字符串子串
slice(startIndex[, endIndex])
substring(startIndex[, endIndex])
对于负数参数,slice() 方法会从后往前数,substring() 方法则将其作为 0 处理(也就是说将忽略它)。
与 concat() 方法一样,slice() 和 substring() 方法都不改变 String 对象自身的值。它们只返回原始的 String 值,保持 String 对象不变 。
toLowerCase()、toLocaleLowerCase()、toUpperCase() 和 toLocaleUpperCase()
toLocaleLowerCase()、toLocaleUpperCase()更安全
代码:
console.log(window.document.domain); console.log(window.document.lastModified); console.log(window.document.referrer); console.log(window.document.title); console.log(window.document.URL); document.write('Hello World!'); document.write('Have a nice day!'); document.writeln('Hello World!'); document.writeln('Have a nice day!'); var test1 = 'hi',test2 = 'hello'; var test = 'hi', age = 25; //没有初始化值的变量也是合法的 var test3; var test; var $test; var $1; var _$te$t2;
ECMAScript 的核心: 函数
return 语句后面的代码都不会执行
注释:如果函数无明确的返回值,或调用了没有参数的 return 语句,那么它真正返回的值是 undefined。
arguments 对象
1. 无需明确命名参数,也可以传入参数
2. 检测参数个数
3. 模拟函数重载
ECMAScript 不会验证传入的参数和定义的参数量是否相等
ECMAScript 的函数实际上是功能完整的对象
Function 对象的length属性声明了函数期望的参数个数
Function 对象的toString()方法可以打印出函数的文本,进行调试
闭包,是指函数可以使用函数之外的变量
使用闭包要小心,因为它们可能会变得非常复杂。
代码:
/** * 信息函数 * @param msg */ function sMessage(msg) { if(msg == 'bye') { // 永远不执行 return; } alert(msg); } sMessage('bye'); function sMessage() { if(arguments[0] == 'bye') { // 永远不执行 return; } alert(arguments[0]); } sMessage('byeccc'); /** * 检测有多少个参数 */ function howManyArgs() { alert(arguments.length); } howManyArgs('some', 'text'); howManyArgs(); howManyArgs('some'); function doAdd(arg1, arg2) { if(arguments.length > 0) { alert('有实参'); }else { alert('无实参'); } } function test() { return 'some text'; } //打印函数所期望的参数个数 console.log(doAdd.length); console.log(test.length); // 简单的闭包实例 var message = 'text'; function myFunction () { console.log(message); } // 函数的内部函数是一个闭包 var iBaseNum = 10; function addNum(iNum1, iNum2) { function doAdd() { return iNum1 + iNum2 + iBaseNum; } return doAdd(); } console.log(addNum(1, 2));
相关文章推荐
- HTML5 Canvas+JS控制电脑或手机上的摄像头实例
- 使用js返回上一页的几段代码
- js鼠标悬浮动画:由一个位置移动到另外一个位置,速度由快变慢[修正版2]
- JavaScript笔记(4)作用域与作用域链
- 12个JavaScript技巧【转】
- Extjs gridPanel 小计保存不上
- ajax和js验证用户登录
- 数字时钟的实现
- JavaScript五子棋
- Ajax结合json在web中的应用
- 不可多得的Javascript(AJAX)开发工具 - Aptana
- js和html的结合方式及js的变量声明和数据类型
- 用Aptana调试JavaScript教程
- JavaScript中关于this关键字的详解
- js验证数字
- Javascript中的字符串连接
- 2016年5月27日下午(妙味课堂js基础-3笔记三(事件))
- net.sf.json.JSONException: Found starting '{' but missing '}' at the end. at character 0 of null
- JS判断浏览器
- 修改jsonb的属性