Object.keys(obj)返回参数obj可被枚举的属性
2016-06-06 10:56
453 查看
Object.keys(obj),返回一个数组,数组里是该obj可被枚举的所有属性。请看示例:
示例一:
function Pasta(grain, width, shape) {
this.grain = grain;
this.width = width;
this.shape = shape;
this.toString = function () {
return (this.grain + ", " + this.width + ", " + this.shape);
}
}
console.log(Object.keys(Pasta)); //console: []
var spaghetti = new Pasta("wheat", 0.2, "circle");
console.log(Object.keys(spaghetti)); //console: ["grain", "width", "shape", "toString"]
示例二:
var arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // console: ["0", "1", "2"]
var obj = { 0 : "a", 1 : "b", 2 : "c"};
console.log(Object.keys(obj)); // console: ["0", "1", "2"]
var an_obj = { 100: "a", 2: "b", 7: "c"};
console.log(Object.keys(an_obj)); // console: ["2", "7", "100"]
var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });
my_obj.foo = 1;
console.log(Object.keys(my_obj)); // console: ["foo"]
示例一:
function Pasta(grain, width, shape) {
this.grain = grain;
this.width = width;
this.shape = shape;
this.toString = function () {
return (this.grain + ", " + this.width + ", " + this.shape);
}
}
console.log(Object.keys(Pasta)); //console: []
var spaghetti = new Pasta("wheat", 0.2, "circle");
console.log(Object.keys(spaghetti)); //console: ["grain", "width", "shape", "toString"]
示例二:
var arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // console: ["0", "1", "2"]
var obj = { 0 : "a", 1 : "b", 2 : "c"};
console.log(Object.keys(obj)); // console: ["0", "1", "2"]
var an_obj = { 100: "a", 2: "b", 7: "c"};
console.log(Object.keys(an_obj)); // console: ["2", "7", "100"]
var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });
my_obj.foo = 1;
console.log(Object.keys(my_obj)); // console: ["foo"]
相关文章推荐
- Swift互用性: 使用Objective-C特性编写Swift类(Swift 2.0版)-b
- Swift互用性:与 Objective-C 的 API 交互(Swift 2.0版更新)-备
- Using Swift with Cocoa and Objective-C(Swift 2.0版):开始--基础设置-备
- NSMutableArray在alloc init之后再调用addobject还是会崩溃的可能原因有哪些? 出现错误:[__NSArrayI addObject:]: unrecognized sel
- Cast to ObjectId …
- IOS Dev Intro - Objective-C Instancetype and id
- Scott Bilas: GDC 2002 Presentation: A Data-Driven Game Object System
- Objective-C ---UIImageView (梳理整理)
- Retrofit 泛型解析遇到com.google.gson.internal.LinkedTreeMap cannot be cast to object
- Objective-C的语法之OOP
- MVC控制器里面使用dynamic和ExpandoObject
- 多线程编程WaitForSingleObject()
- JavaAPI之AccessibleObject
- eclipse基本操作_Object类_equals()_==_hashCode()_getClass()
- Objective-C中#import如何规避多文件结构导致的重复包含;以及@class作用
- Effective Objecttive-C 2.0
- git 错误 fatal: Not a valid object name: 'master'.
- 论文笔记之:Multiple Object Recognition With Visual Attention
- [Objective-C] Copy 和 MutableCopy
- 【原】谈谈对Objective-C中代理模式的误解