Skip to content

Commit a67cf7c

Browse files
committed
Create 第六章
1 parent a352f0e commit a67cf7c

1 file changed

Lines changed: 211 additions & 0 deletions

File tree

‎JavaScriptBook/第六章‎

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
Object.getOwnPropertyDescriptor() //获取某个对象特定属性的描述符
2+
3+
Object.getOwnPropertyDescriptor({x:1},'x')
4+
//Object {value: 1, writable: true, enumerable: true, configurable: true} //不存在返回undefiend
5+
6+
Object.defineProperty() //新建属性或修改属性
7+
8+
var m ={}
9+
Object.defineProperty(m ,'x',{value:1,
10+
writable:true,enumerable:false,configurable:true});
11+
//->Object {x: 1}
12+
m
13+
//->Object {x: 1}
14+
m.x
15+
//->1
16+
Object.keys(m) //属性存在但是不可枚举
17+
//->[]
18+
//修改属性为只读
19+
Object.defineProperty(m,'x',{writable:false})
20+
//Object {x: 1}
21+
22+
m.x=3
23+
//3
24+
m.x
25+
//1
26+
27+
Object.defineProperty(m,'x',{value:23}) //属性可配置,可以进行修改
28+
//Object {x: 23}
29+
m.x
30+
//23
31+
32+
//修改为存取器属性---好像没什么意义,下面会有实例
33+
Object.defineProperty(m,'x',{get: function() {return 0;}})
34+
//Object {x: 0}
35+
m.x
36+
//0
37+
m.x=23;
38+
//23
39+
m.x
40+
//0
41+
42+
43+
同时修改或创建多个属性使用Object.defineProperties()
44+
var p = Object.defineProperties({},{
45+
x:{value:1,writable:true,enumerable:true,configurable:true},
46+
y:{value:1,writable:true,enumerable:true,configurable:true},
47+
r:{
48+
get:function(){return Math.sqrt(this.x*this.x+this.y*this.y) },
49+
enumerable:true,configrable:true
50+
}
51+
})
52+
53+
54+
//6-3复制属性的特征
55+
/**
56+
*不太理解这个是一个什么,属性。
57+
*添加这extend()方法后能够将该对象的所有属性全部复制过去?而且无法单独独调用,属于默认内容
58+
* stackoverflow的答案也只是说这是个能够全部继承并能够成功调用
59+
* http://stackoverflow.com/questions/7033811/copying-attributes-in-javascript
60+
*
61+
*
62+
*/
63+
64+
/**
65+
*给Object.defineProperty添加一个不可枚举的extend()方法
66+
*/
67+
Object.defineProperty( Object.prototype, 'extend', {
68+
writable: true,
69+
enumerable: false, // 将其定义为不可枚举
70+
configurable: true,
71+
value: function(o) {
72+
var names = Object.getOwnPropertyNames(o); // 得到所有的自有属性,包括不可枚举属性
73+
74+
for( var i = 0; i < names; i++ ) { // 遍历它们
75+
if(names[i] in this) continue; // 如果属性已经存在, 则跳过
76+
77+
var desc = Object.getOwnPropertyDescriptor(o, names[i]); // 获得 o 中的属性的描述符
78+
79+
Object.defineProperty(this, names[i], desc); // 用它给 this 创建一个属性
80+
}
81+
}
82+
} );
83+
84+
85+
//6.8.1原型属性
86+
/*
87+
*/
88+
var p = {x:1}
89+
//undefined
90+
var o = Object.create(p);
91+
p.isPrototypeOf(o); //检测p是否是o的原型 true
92+
Object.prototype.isPrototypeOf(o) //p继承自object.prototype =>true
93+
94+
//6.8.2 类属性
95+
//对象的类属性 class attribute 是一个字符串,用以表示对象的类型信息。
96+
//classof()函数可以返回传递给他的任意对象的类
97+
//调用toString()
98+
function classof(o){
99+
if(o ==null) return "Undefined";
100+
if(o == undefined) return "Undefined";
101+
return Object.prototype.toString.call(o).slice(8,-1);
102+
}
103+
104+
//6.8.3可扩展性
105+
//对象的可扩展性用以表示是否可以为对象添加新属性,所有的内置对象和自定义对象都是显式可扩展的
106+
//!*** Object.esExtensible() 该方法被未定义
107+
//!*** [TypeError: Object function Object() { [native code] } has no method 'esExtensible'],
108+
//!*** 使用Object.isExtensible() 发音一样……
109+
//You can set a object to be not extensible, but you cannot set a object to be extensible.
110+
Object.isExtensible() //查询对象是否可扩展
111+
// check if a object is extensible (that is, whether properties can be added)
112+
113+
var uu = {}; // user-defined object
114+
console.log(Object.isExtensible(uu)); // true
115+
116+
var aa = [3,4,5]; // Array
117+
console.log(Object.isExtensible(aa)); // true
118+
119+
var dd = new Date(); // date object
120+
console.log(Object.isExtensible(dd)); // true
121+
122+
Object.preventExtensions()//将对象转换为不可扩展
123+
//一旦被转为不可扩展,就无法转为可扩展
124+
//preventExtensions之影响对象本身,如果给一个不可扩展的对象原型添加属性
125+
//,这个不可扩展的对象同样会继承这些新属性
126+
// set the extensible attribute of a object to false
127+
128+
var u8 = {};
129+
130+
Object.preventExtensions(u8);
131+
132+
u8.pp = 3; // add a property
133+
134+
console.log(u8.pp); // prints 「undefined」. add failed
135+
136+
console.log(Object.isExtensible(u8)); // false
137+
138+
Ojnect.seal() //同preventExtensions ,可以将对象所有自有属性设为不可配置
139+
//对于已经封闭sealed对象,已有属性可设置,不能添加新的或删除使用isSealed检测
140+
Object.preventExtensions(o)
141+
console.log(Object.isExtensible(o)) //=>false
142+
Object.seal(o)
143+
Object {x: 1}
144+
Object.isSealed(o) //true;
145+
146+
Object.freeze()//冻结frozen对象,不可扩展,不可配置,所有属性只读
147+
148+
//嵌套调用
149+
var m = Object.seal(
150+
Object.create(Object.freeze({x:1}),
151+
{y:{value:2,writable:true}})
152+
);
153+
//undefined
154+
m
155+
//Object {y: 2, x: 1}
156+
m.x
157+
//1
158+
m.x=23
159+
//23
160+
m.x //x 为freeze 不可设置
161+
//1
162+
m.y=3
163+
//3
164+
m.y
165+
//3
166+
m.p=23 //m sealed
167+
//23
168+
m
169+
//Object {y: 3, x: 1}
170+
171+
172+
//6.9 对象序列化
173+
//对象序列化 serialization 指对象的状态转换为字符串或还原为字符串
174+
JSON.stringify()
175+
JSON.parse()
176+
t = {x:1,y:{z:[false,null,""]}}
177+
s = JSON.stringify(t)
178+
//"{"x":1,"y":{"z":[false,null,""]}}"
179+
s
180+
//"{"x":1,"y":{"z":[false,null,""]}}"
181+
pp = JSON.parse(s)
182+
//Object {x: 1, y: Object}
183+
184+
//!*** 函数,regExp error,undefined不能序列化,不能序列化的属性将被省略
185+
186+
//6.10对象方法
187+
188+
//6.10.1 toString()
189+
//toString()没有参数,返回一个表示调用这个放的对象的字符串。
190+
pp.toString()
191+
//"[object Object]"
192+
//9.6.3 介绍如何定义重写toString()方法
193+
194+
//6.10.2 toLocaleString()
195+
//返回表示这个对象的本地化字符串????
196+
//Date和Number类对toLocaleString做的定制,可以做本地化转换
197+
//Array 每个数组元素都会调用toLocaleString不调用toString
198+
199+
//6.10.3 toJSon方法
200+
//JSON.stringify()方法会调用toJSON方法 Date.toJSON()……
201+
202+
//6.10.3 valueOf()
203+
//JavaScript需要将对象转换为某种原始值而废字符串的时候才会调用它,尤其是数字
204+
//?????????????
205+
206+
/*
207+
*就是说这些方法,你可能不一定直接使用,但是当操作时,JavaScript会自动的调用它!?
208+
*平时,并不一定能够使用的到…… 或许9.6章回有答案
209+
*/
210+
211+

0 commit comments

Comments
 (0)