码迷,mamicode.com
首页 > 编程语言 > 详细

浅谈 ECMAScript 和 JavaScript

时间:2016-05-27 23:18:58      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

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

代码:

 1 ‘use strict‘;
 2 
 3 /**
 4  * 全局JSON对象
 5  * 序列化JSON.stringify ECMAScript值->JSON
 6  * 反序列化JSON.parse   JSON->ECMAScript值
 7  *
 8  * JSON.parse(text[, reviver)
 9  * JSON.stringify(value[, replacer[, space)
10  */
11 
12 var result = JSON.parse(‘{"a": 1, "b": 2}‘);
13 console.log(result);
14 console.log(result.b);
15 
16 var result = JSON.parse(‘{"a": 1, "b": 2}‘, function (key,value) {
17     if(typeof value == ‘string‘) {
18         console.log(‘string‘);
19         return parseInt(value);
20     }else {
21         console.log(‘other‘);
22         return value;
23     }
24 });
25 
26 console.log(result.b);
27 
28  var nums = {
29     "first": 7,
30     "second": 14,
31     "third": 13
32  };
33 
34 /**
35  * 添加replacer过滤函数操作
36  * space代表缩进空格
37  */
38 var luckyNums = JSON.stringify(nums, function(key, value){
39     if (value == 13) {
40         return undefined;
41     } else {
42         return value;
43     }
44 }, 2);
45 
46 console.log(luckyNums);
47 
48 var cat = {};
49 
50 Object.defineProperty(cat, ‘name‘, {
51     value: ‘Maru‘,
52     writable: false,
53     enumerable: true,
54     configurable: false
55 });
56 
57 Object.defineProperty(cat, ‘skill‘, {
58     value: ‘exploring boxes‘,
59     writable: true,
60     enumerable: true,
61     configurable: false
62 });
63 
64 for(var key in cat) {
65     console.log(key + ‘: ‘ + cat[key]);
66 }
67 
68 console.log(Array.isArray(‘No u‘));
69 console.log(Array.isArray([‘No‘, ‘u‘]));
70 
71 
72 var mike = JSON.stringify({mike: ‘taylor‘});
73 console.log(mike); //{"mike":"taylor"}
74 console.log(typeof mike); //string
75 
76 /**
77  * Array.isArray()直接写在了构造器上,而不是prototype对象上
78  */
79 
80 /**
81  * Function.prototype.bind(thisArg[, arg1[, arg2,...)
82  */
83 function locate() {
84     console.log(this.location);
85 }
86 
87 function Maru(location) {
88     this.location = location;
89 }
90 
91 var maru = new Maru(‘some words‘);
92 var locateMaru = locate.bind(maru);
93 
94 locateMaru();

 

浅谈 ECMAScript 和 JavaScript

标签:

原文地址:http://www.cnblogs.com/lqcdsns/p/5536247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!