视频学习地址:

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的属性操作的更多相关文章

  1. js object 对象 属性和方法的使用

    //object 对象 属性和方法的使用 var person = new Object(); person.name="张海"; person.age="; perso ...

  2. js Object的属性 Configurable,Enumerable,Writable,Value,Getter,Setter

    对象的数据属性 Configurable,Enumerable,Writable,Value var person = {} Object.defineProperty(person,'name',{ ...

  3. JS基础之属性操作注意事项

    1.js中注意问题 font-size 改成fontSize padding-top 改成paddingTop 2.js动态添加Class class    改成className 3.oInp.ty ...

  4. JavaScript基础学习日志(1)——属性操作

    JS中的属性操作: 属性操作语法 属性读操作:获取 实例:获取Input值 实例:获取select值 字符串连接 属性写操作:修改.添加 实例:修改value值 实例:添加图片的src地址 inner ...

  5. js学习笔记2---HTML属性操作

    1.HTML属性操作:读.写 属性名 属性值   2.属性读操作:获取.找到 a) 语法:元素.属性名 如:document.getElementById(“btn”).value; b) 字符串的连 ...

  6. 第二十一课:js属性操作的兼容性问题

    上一课主要讲了属性的概念,用法,固有属性和自定义属性的区别,class属性操作的方法等,这一课主要讲一些有关属性操作的兼容性问题. IE6-IE8在一些表示URL的属性会返回补全的改过编码的路径,比如 ...

  7. [妙味JS基础]第一课:属性操作、图片切换、短信发送模拟

    知识点总结 HTML的属性操作:读.写 元素.属性名 => “读” 元素.属性名=新的值 => “写” 例如: oBtn.value => “读” oBtn.value='按钮' = ...

  8. js动态参数作为Object的属性取值

    js动态参数作为Object的属性取值var myObj = {"a":1,"b":2};var a = 'a';myObj[a] 就可以获取到 属性a的值了

  9. GSAP JS基础教程--TweenLite操作元素的相关属性

    今天来学习用TweenLite操作元素的各种属性,以Div为例,其他元素的操作也是一样的,只是可能一些元素有它们的特殊属性,就可能不同罢了.   代码里用详细注释,我就不再重复啦,大家看代码就可以啦! ...

随机推荐

  1. velocity 教程

    1,<title> $!{product.name} - $!{title} $!{about.title} - $!{title} $!{news.title} - $!{title} ...

  2. CSS概要

    CSS概要 laiqun@msn.cn Contents 1. css的引入 2. css的选择器及效果图 3. css 盒模型 4. css 浮动 4.1. 浮动的作用: 4.2. 浮动的影响: 5 ...

  3. centos主机信任

    一.实现原理 使用一种被称为"公私钥"认证的方式来进行ssh登录."公私钥"认证方式简单的解释是: 首先在客户端上创建一对公私钥(公钥文件:~/.ssh/id_ ...

  4. CocoaAsyncSocket框架的简单封装

    在iOS开发中使用socket(CFNetwork),一般都是用第三方库AsyncSocket. 参考博客:http://my.oschina.net/worldligang/blog/396881? ...

  5. html 背景透明文字不透明

    .alpha{ width: 100px; height: 100px; color: #fff; background: rgba(0, 0, 0, .3); filter: progid:DXIm ...

  6. C# 垃圾回收机制(转)

    摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...

  7. WPF Template模版之寻找失落的控件【三】

    “井水不犯河水”常用来形容两个组织之间界限分明.互不相干,LogicTree与控件内部这颗小树之间就保持着这种关系.换句话说,如果UI元素树上有个X:Name=“TextBox1”的控件,某个控件内部 ...

  8. iOS 判断奇偶数

    if (_bigUrlArray.count%2==0) {//如果是偶数 a = i*(_bigUrlArray.count/count);//每个线程图片初始数 b = (i+1)*(_bigUr ...

  9. [转]动态添加Fragments

    本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...

  10. java 对象的上转型对象(父类)

    Example5_10.java class 类人猿 { void crySpeak(String s) { System.out.println(s); } } class People exten ...