是否可以解析javascript中对象的所有public变量?
Is it possible to parse all public variables of an object in javascript?
假设我有一个包含一组 public 变量和私有变量的对象
function myObj()
{
var private1 = 1;
var private2 = 2;
this.func = function(){console.log('anything');};
this.public1 = 3;
this.public2 = '4';
}
有没有办法创建一个可以解析对象并检索 public 变量的名称、值和类型的函数。
这个函数的原型是:
parseObj(object)
控制台结果将是:
>object has public1 with value 3 of type Number
>object has public2 with value 4 of type String
试试这个:
Object.entries(object).forEach((prop) => {
const [name, value] = prop;
console.log(`object has ${name} with value of ${value} and type ${typeof(value)}`)
})
我在控制台中得到这个:
// object has func with value of function (){console.log('anything');} and type function
// object has public1 with value of 3 and type number
// object has public2 with value of 4 and type string
您可以很容易地使用 for in loop:
来创建这样的函数
function parseObj(object) {
for(var name in object) {
if(object.hasOwnProperty(name)) {
// ignoring methods
if(typeof object[name] !== 'function') {
console.log('object has '+name+' with value '+object[name]+' of type '+typeof object[name]);
}
}
}
}
您没有在 OP 中包含 "not ECMAScript 2015" 条件。如果 ECMAScript 5.1 没问题,那么 Object.keys 将 return 拥有属性(有一个 polyfill on MDN)。
没有内置函数可以准确地 returns 一个值的类型(尽管您自己可以很容易地做到这一点)。 typeof returns 有用但不匹配类型的值,例如没有 "function" 类型但是:
typeof function(){}
returns "function"。此外,宿主对象可能 return 各种值(例如 "unknown")。
function myObj()
{
var private1 = 1;
var private2 = 2;
this.func = function(){console.log('anything');};
this.public1 = 3;
this.public2 = '4';
}
var obj = new myObj();
function showProps(obj) {
Object.keys(obj).forEach(function(key) {
console.log('object has ' + key + ' with value ' + obj[key] +
' with typeof ' + (typeof obj[key]));
});
}
showProps(obj);
假设我有一个包含一组 public 变量和私有变量的对象
function myObj()
{
var private1 = 1;
var private2 = 2;
this.func = function(){console.log('anything');};
this.public1 = 3;
this.public2 = '4';
}
有没有办法创建一个可以解析对象并检索 public 变量的名称、值和类型的函数。
这个函数的原型是:
parseObj(object)
控制台结果将是:
>object has public1 with value 3 of type Number
>object has public2 with value 4 of type String
试试这个:
Object.entries(object).forEach((prop) => {
const [name, value] = prop;
console.log(`object has ${name} with value of ${value} and type ${typeof(value)}`)
})
我在控制台中得到这个:
// object has func with value of function (){console.log('anything');} and type function
// object has public1 with value of 3 and type number
// object has public2 with value of 4 and type string
您可以很容易地使用 for in loop:
来创建这样的函数function parseObj(object) {
for(var name in object) {
if(object.hasOwnProperty(name)) {
// ignoring methods
if(typeof object[name] !== 'function') {
console.log('object has '+name+' with value '+object[name]+' of type '+typeof object[name]);
}
}
}
}
您没有在 OP 中包含 "not ECMAScript 2015" 条件。如果 ECMAScript 5.1 没问题,那么 Object.keys 将 return 拥有属性(有一个 polyfill on MDN)。
没有内置函数可以准确地 returns 一个值的类型(尽管您自己可以很容易地做到这一点)。 typeof returns 有用但不匹配类型的值,例如没有 "function" 类型但是:
typeof function(){}
returns "function"。此外,宿主对象可能 return 各种值(例如 "unknown")。
function myObj()
{
var private1 = 1;
var private2 = 2;
this.func = function(){console.log('anything');};
this.public1 = 3;
this.public2 = '4';
}
var obj = new myObj();
function showProps(obj) {
Object.keys(obj).forEach(function(key) {
console.log('object has ' + key + ' with value ' + obj[key] +
' with typeof ' + (typeof obj[key]));
});
}
showProps(obj);