标签:javascript 对象 继承
直接创建
var obj = { name: "mike", age: 10 }
用new创建
var ob = new Date();
ECMAScript5有个新的方法 Object.create()
其实这个方法就是继承某一个对象
var obj = { name: "mike", age: 10 }; var newobj = Object.create(obj); console.log(newobj.name);
1
var a = { name: "mike", name: "john" } console.log(a["name"]);不小心的把属性名写成一样 结果是john也就是说当遇到属性名一样的情况下. 按照最后一个出现的算, 注意当开始use strict的时候会报错
2
var a = {}; a.x = 1; var p = Object.create(a); p.y = 2; console.log(p.x);当你访问对象的属性没有时候,他会沿着原型链继续向上找,
3
var a = {}; a.x = 1; var p = Object.create(a); p.y = 2; console.log("x" in p); //true
4
var a = {}; a.x = 1; var p = Object.create(a); p.y = 2; console.log("x" in p); //true console.log(p.hasOwnProperty("x")); //false检查自有属性
5
var a = { x:1, get getx() {return this.x;}, set setx(arg) { return this.x = arg; } } console.log(a.x); a.x = 100; console.log(a.x);
6
var a = { x:1, get getx() {return this.x;}, set setx(arg) { return this.x = arg; } } Object.freeze(a); a.x = 100; console.log(a.x); //1
标签:javascript 对象 继承
原文地址:http://blog.csdn.net/atvance016/article/details/40709505