var point = {x: 0, y: 0 };
var empty = {};
va point = {x: 0, y: 0 };
var book = {
"main title": "Javascript",
"sub-title": "The definitive Guide",
"for": "all audience",
author: {
firstName: "Davide",
lastName: "Flanagan"
}
};
function Person(){
//构造函数
}
var person = new Person();
var a = new Array(); var d = new Date(); var r = new RegExp(“js”);
var o1 = Object.create({x: 1, y: 2 }); //原型为Object.prototype
var o2 = Object.create(null); //没有原型
var o3 = Object.create(Object.prototype);
function Person(){
}
var o4 = Object.create(Person.prototype);
ar author = book.author; //正确 var name = author.surname; //正确 var title = book[“main title”]; //正确 var className = book.class; //错误
function getvalue(portfolio){
var total = 0.0;
for(stock in portolio){
var shares = portolio[stock];
var price = getquote(stock);
total += shares * price;
}
return total;
}
function inherit(p){
if (p == null) throw TypeError(); //p是一个对象,大不能是null
if(Object.create){
return Object.create(p); //直接使用Object.create方法
}
var t = typeof p;
if(t !== "object" && t !== "function") throw TypeError();
function f() {};
f.prototype = p; //将其原型属性设置为p
return new f();
}
var o = {}; //o从Object.prototype继承对象属性
o.x = 1; //给o定义x属性
var p = inherit(o); //p继承o和Object.prototype
p.y = 2; //p定义属性y
var q = inherit(p); //q继承p、o和Object.prototype
q.z = 3; //给q定义属性z
var s = q.toString(); //toString继承自Object.prototype
q.x + q.y // => 3:x和y分别继承自o和p
delete book.author; delete book[“main title”];
ar o = {x: 1};
delete o.x; //删除x,返回true
delete o.x; //x已经不存在了,什么都没做,返回true。
delete o.toString; //什么都没做,返回true。
delete不能删除可配置型为false的属性。某些内置对象的属性是不可配置的,比如通过变量声明和函数声明创建的全局对象的属性:
delete Object.prototype //不能删除,属性是不可配置的
var x = 1;
delete this.x; //不能删除这个属性
function f() {}
delete this.f; //不能删除全局函数
var o = {x: 1};
"x" in o; //true:x是o的属性
"y" in o; //false:y不是o的属性
"toString" in o; //true:o继承toString属性
var o = {x: 1};
o.hasOwnProperty("x"); //true:o有一个自由属性x
o.hasOwnProperty("y"); //false:o中不存在属性y
o.hasOenProperty("toString"); //false:toString是继承属性
var o = inherit({y: 2});
o.x = 1;
o.propertyIsEnumerable("x"); //true: o有一个可枚举属的自有属性x
o.propertyIsEnumerable("y"); //false:y是继承来的
Object.prototype.propertyIsEnumerable("toString"); //false:不可枚举
var o = {x: 1, y: 2, z: 3}; //三个可枚举的自有属性
o.propertyIsEnumeable("toString"); //false,不可枚举
for (p in o) //遍历属性
console.log(p); //输出x、y和z,不会输出toString
for(p in o){
if(!o.hasOwnProperty(p)) continue;
if(typeof o[p] === "function") continue;
}
/*
* 把p中的可枚举属性复制到o中,并返回o
* 如果o和p含同名属性,则覆盖o中的属性
* 这个函数并不处理getter和setter以及复制属性
*/
function extend(o, p){
for(prop in p){ //遍历p中的所有属性
o[prop] = p[prop]; //将属性添加到o中
}
return o;
}
var o = {
//普通的数据属性
data_prop: 1,
//存取器属性都是成对定义的函数
get accessor_prop(){/* 这里是函数体 */},
set accessor_prop(value){}
};
var p = {
//x和y是普通的可读写数据属性
x: 1.0,
y: 1.0,
//r是可读写的存取器属性,它有getter和setter
get r(){return Math.sqrt(this.x * this.x + this.y * this.y); },
set r(newValue){
var oldValue = Math.sqrt(this.x * this.x + this.y * this);
var ratio = newValue / oldValue;
this.x *= ratio;
this.y *= ratio;
},
//theta是只读存取器属性,只有getter方法
get theta() { return Math.atan2(this.y, this.x); }
};
var q = inherit(p); q.x = 1, q.y = 1; console.log(q.r); cosole.log(q.theta);
//返回{value: 1, writable: true, enumerable: true, configurable: true}
Object.getOwnProeprtyDescriptor({x: 1},"x");
//查询上文中定义的random对象的octet属性
//返回{get: /*func */, set: undefined, enumerable: true, configurable: true}
Object.getOwnPropertyDesciptor(random, "octet");
//对于继承属性和不存在属性,返回undefined
Object.getOwnPropertyDesciptor({}, "x");
Object.getOwnPropertyDesciptor({}, "toString");
// 属性是存在的,但不可枚举
o.x; //=> 1
Object.keys(o) //=> []
//现在对属性x做修改,让它变成只读
Object.defineProperty(o, "x", {writable: true });
//视图更改这个属性的值
o.x = 2; //操作失败但不报错,而在严格模式中抛出类型错误异常
//属性依然是可配置的,因此可通过这种方式对它进行修改:
Object.defineProperty(o, "x", {value: 2 });
o.x //=> 2
//现在将x从数据属性修改为存取器属性
Object.defineProperty(o, "x", { get: function() {return 0;} });
o.x // => 0
var p = Object.defineProperties({}, {
x: { value: 1, writable: true, enumerable: true, configurable: true},
y: { value: 2, writable: true, enumerable: true, configurable: true},
r: {
get: function(){ return Math.sqrt(this.x * this.x + this.y * this.y); },
enumerable: true,
configurable: true
}
});
var o = {};
o.__defineGetter__("x", function(){return 0;});
o.__defineSetter__("y", function(value){console.log("set value:" + value);});
var p = {x: 1}; //定义一个原型对象
var o = Object.create(p); //使用这个原型创建一个对象
p.isPrototypeOf(o); //=> true,o继承自p
Object.prototype.isPrototypeOf(o) //=> true, p继承自Object.prototype
function classof(o){
if(o === null) return "Null";
if(o === undefined) return "Undefined";
return Object.prototype.toString.call(o).slice(8, -1);
}
o = {x: 1, y: {z: [false, null, ""]}}; //定义一个测试对象
s = JSON.stringify(o); //{"x":1,"y":{"z":[false,null,""]}}
p = JSON.parse(s); //p是o的深拷贝
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有