js - object的属性操作
视频学习地址:
http://www.imooc.com/video/6002
原文PPT下载地址:
http://img.mukewang.com/down/54c5ec1a000141f100000000.zip
1、属性删除
var person = {age : 28, title : 'fe'};
delete person.age; // true
delete person['title']; // true
person.age; // undefined
delete person.age; // true
delete Object.prototype; // false,
var descriptor = Object.getOwnPropertyDescriptor(Object, 'prototype');
descriptor.configurable; // false
2、属性检测
var cat = new Object;
cat.legs = 4;
cat.name = "Kitty"; 'legs' in cat; // true
'abc' in cat; // false
"toString" in cat; // true, inherited property!!! cat.hasOwnProperty('legs'); // true
cat.hasOwnProperty('toString'); // false cat.propertyIsEnumerable('legs'); // true
cat.propertyIsEnumerable('toString'); // false if (cat && cat.legs) {
cat.legs *= 2;
} if (cat.legs != undefined) {
// !== undefined, or, !== null
} if (cat.legs !== undefined) {
// only if cat.legs is not undefined
}
3、属性枚举
var o = {x : 1, y : 2, z : 3};
'toString' in o; // true
o.propertyIsEnumerable('toString'); // false 说明原型链上Object.prototype中的toString()方法不可枚举
var key;
for (key in o) {
console.log(key); // x, y, z
}
var obj = Object.create(o);
obj.a = 4;
var key;
for (key in obj) {
console.log(key); // a, x, y, z
}
var obj = Object.create(o);
obj.a = 4;
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key); // a 只枚举自身的属性
}
}
5、属性的getter / setter
var man = {
name : 'Bosn',
weibo : '@Bosn',
get age() {
return new Date().getFullYear() - 1988;
},
set age(val) {
console.log('Age can\'t be set to ' + val);
}
}
console.log(man.age); //
man.age = 100; // Age can't be set to 100
console.log(man.age); // still 27
var man = {
weibo : '@Bosn',
$age : null,
get age() {
if (this.$age == undefined) {
return new Date().getFullYear() - 1988;
} else {
return this.$age;
}
},
set age(val) {
val = +val;
if (!isNaN(val) && val > 0 && val < 150) {
this.$age = +val;
} else {
throw new Error('Incorrect val = ' + val);
}
}
}
console.log(man.age); //
man.age = 100;
console.log(man.age); // 100;
man.age = 'abc'; // error:Incorrect val = NaN
6、getter / setter 和 defineProperty
function foo() {}
Object.defineProperty(foo.prototype, 'z',
{get : function(){return 1;}});
var obj = new foo();
obj.z; //
obj.z = 10;
obj.z; // still 1
Object.defineProperty(obj, 'z',
{value : 100, configurable: true});
obj.z; // 100;
delete obj.z;
obj.z; // back to 1
var o = {};
Object.defineProperty(o, 'x', {value : 1}); // writable=false, configurable=false
var obj = Object.create(o);
obj.x; //
obj.x = 200;
obj.x; // still 1, can't change it
Object.defineProperty(obj, 'x', {writable:true, configurable:true, value : 100});
obj.x; //
obj.x = 500;
obj.x; //
7、属性的权限设置 Object.defineProperty
var person = {};
Object.defineProperty(person, 'name', {
configurable : false,
writable : false,
enumerable : true,
value : "Bosn Ma"
});
person.name; // Bosn Ma
person.name = 1;
person.name; // still Bosn Ma
delete person.name; // false
Object.defineProperties(person, {
title : {value : 'fe', enumerable : true},
corp : {value : 'BABA', enumerable : true},
salary : {value : 50000, enumerable : true, writable : true}
});
Object.getOwnPropertyDescriptor(person, 'salary');
// Object {value: 50000, writable: true, enumerable: true, configurable: false}
Object.getOwnPropertyDescriptor(person, 'corp');
// Object {value: "BABA", writable: false, enumerable: true, configurable: false}
Object.defineProperties(person, {
title : {value : 'fe', enumerable : true},
corp : {value : 'BABA', enumerable : true},
salary : {value : 50000, enumerable : true, writable : true},
luck : {
get : function() {
return Math.random() > 0.5 ? 'good' : 'bad';
}
},
promote : {
set : function (level) {
this.salary *= 1 + level * 0.1;
}
}
});
Object.getOwnPropertyDescriptor(person, 'salary');
// Object {value: 50000, writable: true, enumerable: true, configurable: false}
Object.getOwnPropertyDescriptor(person, 'corp');
// Object {value: "BABA", writable: false, enumerable: true, configurable: false}
person.salary; //
person.promote = 2;
person.salary; //

js - object的属性操作的更多相关文章
- js object 对象 属性和方法的使用
//object 对象 属性和方法的使用 var person = new Object(); person.name="张海"; person.age="; perso ...
- js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter
对象的数据属性 Configurable,Enumerable,Writable,Value var person = {} Object.defineProperty(person,'name',{ ...
- JS基础之属性操作注意事项
1.js中注意问题 font-size 改成fontSize padding-top 改成paddingTop 2.js动态添加Class class 改成className 3.oInp.ty ...
- JavaScript基础学习日志(1)——属性操作
JS中的属性操作: 属性操作语法 属性读操作:获取 实例:获取Input值 实例:获取select值 字符串连接 属性写操作:修改.添加 实例:修改value值 实例:添加图片的src地址 inner ...
- js学习笔记2---HTML属性操作
1.HTML属性操作:读.写 属性名 属性值 2.属性读操作:获取.找到 a) 语法:元素.属性名 如:document.getElementById(“btn”).value; b) 字符串的连 ...
- 第二十一课:js属性操作的兼容性问题
上一课主要讲了属性的概念,用法,固有属性和自定义属性的区别,class属性操作的方法等,这一课主要讲一些有关属性操作的兼容性问题. IE6-IE8在一些表示URL的属性会返回补全的改过编码的路径,比如 ...
- [妙味JS基础]第一课:属性操作、图片切换、短信发送模拟
知识点总结 HTML的属性操作:读.写 元素.属性名 => “读” 元素.属性名=新的值 => “写” 例如: oBtn.value => “读” oBtn.value='按钮' = ...
- js动态参数作为Object的属性取值
js动态参数作为Object的属性取值var myObj = {"a":1,"b":2};var a = 'a';myObj[a] 就可以获取到 属性a的值了
- GSAP JS基础教程--TweenLite操作元素的相关属性
今天来学习用TweenLite操作元素的各种属性,以Div为例,其他元素的操作也是一样的,只是可能一些元素有它们的特殊属性,就可能不同罢了. 代码里用详细注释,我就不再重复啦,大家看代码就可以啦! ...
随机推荐
- MVC4相关Razor语法以及Form表单(转载)
Razor的布局(Layout) 默认建的工程都自带的了一个_ViewStart.cshtml文件,文件里面的代码如下: @{ Layout = "~/Views/Shared/_Layou ...
- javascript 为啥不用instanceof检测数组,这里有一个示例坑
前些天写js遇到了一个instanceof的坑,我们的页面中有一个iframe,我在index页面中计算得到了一个array,然后需要传递到Flight页面 这个嵌套的iframe中的一个函数(Sea ...
- centos配置samba
一.samba服务器的安装与配置 [root@localhost ~]# yum -y install samba samba-common samba-client samba服务器所 ...
- 查看SQL Server 2008的版本及位数
如何查看SQL Server 2008的版本及位数及SP版本: 登录SQL Server,找到“SQL查询分析器”,输入“Select @@version”,运行,即可看出版本及SP版本. 该方法适用 ...
- 【转】哦,mysql 的其它发行版本Percona, mariadb
原文:http://geek.csdn.net/news/detail/130146 2016年11月25日,沃趣科技"智慧应用 数据先行"2016产品发布会暨新三板挂牌庆祝会在杭 ...
- Learning Java IO indexes
I/O Streams, it simplifies I/O operations, write a whole object out to stream & read back. File ...
- 单元测试、自动化测试、接口测试过程中的Excel数据驱动(java实现)
import java.io.FileInputStream;import java.io.InputStream;import java.util.HashMap;import java.util. ...
- WebSocket学习笔记——无痛入门
WebSocket学习笔记——无痛入门 标签: websocket 2014-04-09 22:05 4987人阅读 评论(1) 收藏 举报 分类: 物联网学习笔记(37) 版权声明:本文为博主原 ...
- Android接收wifi路由器发送过来的一组字节数据
1.字节数组转换为字符串 byte[] byBuffer = new byte[20];... ...String strRead = new String(byBuffer);strRead = S ...
- panel 绑定鼠标滚轮事件
void formsample_mousewheel(object sender, MouseEventArgs e) { //获取光标位置 Point mousepoint = new Point( ...