ECMAScript5之Object学习笔记(一)
随着IE的逐步追赶,目前到IE11已经能够很好的支持ECMAScript5标准了,其他的现代浏览器像firefox,chrome,opera就更不用说了。
再加上nodejs使得javascript在后台开发中得到施展的舞台,这很自然的激发了我对ECMAScript5相关的特性的求知欲望。
以此展开,写一个ECMAScript5新特性的学习笔记。
先来看看Object
Object.create(proto[, propertiesObject])
create方法通过指定的原型对象(prototype object)和属性(properties)来创建一个新的对象。
proto:即为新创建对象的prototype
propertiesObject:带属性描述的属性对象(姑且这么翻译)
create方法能够让我们方便的通过prototype原型链来实现“继承”:
// super class
var Human = function(cfg) {
this.gender = cfg.gender;
this.name = cfg.name;
}; Human.prototype.sayHello = function() {
console.log('Hello, I am ' + this.name);
}; // sub class
var Citizen = function(cfg) {
this.country = cfg.country;
// call super constructor
Human.call(this, cfg);
}; // prototype chain inherit
Citizen.prototype = Object.create(Human.prototype);
// override constructor
Citizen.prototype.constructor = Citizen; // overwrite super class method
Citizen.prototype.sayHello = function() {
console.log('Hello, I am ' + this.name + ' from ' + this.country);
}; var h = new Human({
gender: 'female',
name: 'lucy'
});
h.sayHello();
console.log( h instanceof Human ); var c = new Citizen({
gender: 'male',
name: 'Andrew',
country: 'USA'
});
c.sayHello();
console.log( c instanceof Citizen);Object.defineProperty(obj, prop, descriptor)
defineProperty方法直接在一个对象上定义一个新属性,或者更改一个已存在的属性,返回对象本身。
obj:对象
prop: 属性名
descriptor: 属性描述对象
用code来做直观说明:
/*
enumerable 是否可列举 默认:false
writable 是否可写 默认:false
configurable是否可配置 默认:false
value 默认:undefined 访问器
get 默认:undefined
set 默认:undefined
*/
var o = {}; // enumerable 是否可列举
Object.defineProperty(o, 'a', {
value: 1,
enumerable: false
}); Object.defineProperty(o, 'b', {
value: 2,
enumerable: true
}); Object.defineProperty(o, 'c', {
value: 3
// enumerable默认false
}); o.d = 4; for(var prop in o) {
// 输出b、d
console.log(prop);
} // writable 是否可写(更改值)
// 这个特性在定义常量时比较有用
Object.defineProperty(o, 'e', {
value: 10,
writable: false
}); console.log(o.e); //
o.e = 15; // 一般没错误抛出,若是在strict mode(严格模式下)会抛出错误
console.log(o.e); // // configurable 是否可配置
Object.defineProperty(o, 'f', {
configurable: true,
get: function() { return 10; }
}); // 如果configurable为false,那么下面这些redefine(重定义)操作都会报TypeError
// 如果configurable为true,那么下面这些操作均能成功,delete操作也能删除e属性
Object.defineProperty(o, 'f', { configurable: true }); // TypeError
Object.defineProperty(o, 'f', { enumerable: true }); // TypeError
Object.defineProperty(o, 'f', { set: function() {} }); // TypeError
Object.defineProperty(o, 'f', { get: function() {return 10;}}); //TypeError
Object.defineProperty(o, 'f', { value: 12}); // TypeError console.log(o.f); //
delete o.f; // nothing happens
console.log(o.f); // // get, set访问器
// 注意:有get、set的情况下,不能同存在value,writable属性,否则会报错
var variable = 10;
Object.defineProperty(o, 'g', {
get: function() {
return variable;
},
set: function(val) {
variable = val;
}
}); console.log(o.g); //
o.g = 14; // set(14)
console.log(o.g); // // 补充:
o.h = 1;
// 相当于:
Object.defineProperty(o, 'h', {
value: 1,
writable: true,
configurable: true,
enumerable: true
}) ; Object.defineProperty(o, 'h', { value: 1 });
// 相当于:
Object.defineProperty(o, 'h', {
value: 1,
writable: false,
configurable: false,
enumerable: false
});object.defineProperties(obj, props)
这个与defineProperty的区别在于一下子可以定义多个属性,就再不展开了。
第一部分暂时到这里结束 : ),感兴趣的同学可以直接去MDN进行了解,请点击这里。
ECMAScript5之Object学习笔记(一)的更多相关文章
- ECMAScript5之Object学习笔记(三)
第三部分继续... Object.getOwnPropertyDescriptor(obj, prop) 获取一个对象的属性描述符 根据"Own"这个词我们可以猜到,prop只能是 ...
- ECMAScript5之Object学习笔记(二)
继续第二部分 Object.freeze(obj) 看字面意思就是“把一个对象冻结”. 下面我们来看个简单的例子以作说明: // a person instance var person = { na ...
- Object学习笔记
<script type="text/javascript"> function forEach(o){ var html =""; for(var ...
- [javascript|基本概念|Object]学习笔记
对象:数据和功能的集合 创建对象:new 对象类型名称 e.g.: var o = new Object(); 或 var o = new Object(省略(),不推荐) 或 var o = {}( ...
- JavaScript Object学习笔记二
Object.create(proto, [propertiesObject])//创建对象,使用参数一来作为新创建对象的__proto__属性,返回值为在指定原型对象上添加自身属性后的对象 //参数 ...
- JavaScript Object学习笔记一
Object.assign(target, source1, source2, ...)//用于对象的复制合并(同名属性后覆盖前)或拷贝(拷贝自身可枚举属性,不拷贝继承属性或不可枚举属性),将sour ...
- Object C学习笔记24-关键字总结
学习Object C也有段时间了,学习的过程中涉及到了很多Object C中的关键字,本文总结一下所涉及到的关键字以及基本语法. 1. #import #import <> 从syste ...
- Object C学习笔记22-#define 用法
上一篇讲到了typedef 关键字的使用,可以参考文章 Object C 学习笔记--typedef用法 .而在c中还有另外一个很重要的关键字#define. 一. #define 简介 在C中利用预 ...
- Object C学习笔记21-typedef用法
在上一章的学习过程中遇到了一个关键字typedef,这个关键字是C语言中的关键字,因为Object C是C的扩展同样也是支持typedef的. 一. 基本作用 typedef是C中的关键字,它的主要作 ...
随机推荐
- 【WIN10】Toast 通知
DEMO下載:http://yunpan.cn/cFSLZQf5ePeTV 访问密码 1fce 1.顯示通知 使用xml確定通知內容. string xml = "<toast la ...
- php常见网络攻击及防御方法
常见的Web攻击分为两类:一是利用Web服务器的漏洞进行攻击,如CGI缓冲区溢出,目录遍历漏洞利用等攻击;二是利用网页自身的安全漏洞进行攻击,如SQL注入,跨站脚本攻击等.下面这篇文章主要介绍了PHP ...
- 通过TortoiseGit上传项目到GitHub
1.安装msysgit和TortoiseGit : 2.TortoiseGit 设置: (1).确保安装成功: (2).设置用户名和邮箱: 3.登陆github并进入设置页面: 4.添加 SSH Ke ...
- 80X86指令总结
一.数据传送指令 指令名称 汇编语句格式 功能 影响标志位 传送move data mov opd, ops (ops) → opd:分为主存储器.通用寄存器.段寄存器,不可同时使用主存储器,类型要匹 ...
- Python168的学习笔记6
如何派生内置不可变类型并修改实例化行为. 个人理解,如何派生出自己想要的类. class IntTuple(tuple): def __new__(cls,iterable): g = (x for ...
- SPOJ 10234. Here Be Dragons
The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the oth ...
- spring---aop(10)---Spring AOP中AspectJ
写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...
- IDA Supported Processors
IDA supports more than 50 families of processors. The source code of some of the processor modules i ...
- Android 官方文档:(一)动画和图像 —— 1.5 画布和画图
The Android framework APIs provides a set 2D drawing APIs that allow you to render your owncustom gr ...
- 用资源管理器右键编译 Visual Studio 解决方案文件
每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...