对象 returns 拥有 属性 并保留方法
Object returns own property and preserves methods
我有一个包装一些数据的对象:
function Obj1() {
var _foo = 'bar'
this.obj2 = {
'b': 'c'
}
this.method = function() {
return _foo
}
}
var obj1 = new Obj1()
现在,当我调用 console.log(obj1);
时,我希望它向我显示对象 obj2
的内容。诀窍是我仍然需要能够调用 obj1.method
并获取 _foo
的值。如果可能的话,我该怎么做?
我的想法是像 getter 这样的东西是合适的,但不知道在哪里以及如何分配一个。
如果我没理解错的话,你可以用prototype
例子
function Obj1() {
this.obj2 = {
'b': 'c'
}
}
Obj1.prototype.method = function() {
return 'bar';
}
var obj1 = new Obj1();
//prints only properties
console.log(obj1);
//prints method result
console.log(obj1.method())
据我了解,您是想隐藏 method
属性。为此,请使用 Object.defineProperty
。函数将不会被记录,因为 enumerable
属性 默认情况下是 false
,这会阻止 属性 在 console.log
中显示。
function Obj1() {
var _foo = 'bar'
this.obj2 = {
'b': 'c'
}
Object.defineProperty(this.obj2, 'method', {
value: function() {
return _foo;
}
});
return this.obj2;
}
var obj1 = new Obj1()
console.log(obj1);
console.log(obj1.method());
自从您致电 new Obj1()
。结果变量 var obj1
是一个 class 对象而不是函数,要获得 obj2
的值,您必须在控制台日志中调用 obj1.obj2
。如果你想让 obj1 保存 obj2 的值。然后使用下面的代码
function Obj1() {
var obj2 = {
'b': 'c'
}
return this.obj2;
}
var obj1 = Obj1();
console.log(obj1);
这将在控制台日志中为您提供所需的结果,但该对象将不再是 class 对象,并且只有 obj2 的值。
坚持你原来的片段,工厂看起来是个不错的选择:
function factory() {
var foo = 'bar';
var props = { b: 'c'};
var proto = {
method: function() { return foo; }
};
var obj = Object.create(proto);
Object.assign(obj, props);
return obj;
}
var obj = factory();
console.log(obj); // {b: 'c'}
console.log(obj.method()) // 'foo'
您甚至可以将 props 作为参数传递,以使用访问私有成员的“unenumerable”方法获得更灵活的生成对象的方式。
我有一个包装一些数据的对象:
function Obj1() {
var _foo = 'bar'
this.obj2 = {
'b': 'c'
}
this.method = function() {
return _foo
}
}
var obj1 = new Obj1()
现在,当我调用 console.log(obj1);
时,我希望它向我显示对象 obj2
的内容。诀窍是我仍然需要能够调用 obj1.method
并获取 _foo
的值。如果可能的话,我该怎么做?
我的想法是像 getter 这样的东西是合适的,但不知道在哪里以及如何分配一个。
如果我没理解错的话,你可以用prototype
例子
function Obj1() {
this.obj2 = {
'b': 'c'
}
}
Obj1.prototype.method = function() {
return 'bar';
}
var obj1 = new Obj1();
//prints only properties
console.log(obj1);
//prints method result
console.log(obj1.method())
据我了解,您是想隐藏 method
属性。为此,请使用 Object.defineProperty
。函数将不会被记录,因为 enumerable
属性 默认情况下是 false
,这会阻止 属性 在 console.log
中显示。
function Obj1() {
var _foo = 'bar'
this.obj2 = {
'b': 'c'
}
Object.defineProperty(this.obj2, 'method', {
value: function() {
return _foo;
}
});
return this.obj2;
}
var obj1 = new Obj1()
console.log(obj1);
console.log(obj1.method());
自从您致电 new Obj1()
。结果变量 var obj1
是一个 class 对象而不是函数,要获得 obj2
的值,您必须在控制台日志中调用 obj1.obj2
。如果你想让 obj1 保存 obj2 的值。然后使用下面的代码
function Obj1() {
var obj2 = {
'b': 'c'
}
return this.obj2;
}
var obj1 = Obj1();
console.log(obj1);
这将在控制台日志中为您提供所需的结果,但该对象将不再是 class 对象,并且只有 obj2 的值。
坚持你原来的片段,工厂看起来是个不错的选择:
function factory() {
var foo = 'bar';
var props = { b: 'c'};
var proto = {
method: function() { return foo; }
};
var obj = Object.create(proto);
Object.assign(obj, props);
return obj;
}
var obj = factory();
console.log(obj); // {b: 'c'}
console.log(obj.method()) // 'foo'
您甚至可以将 props 作为参数传递,以使用访问私有成员的“unenumerable”方法获得更灵活的生成对象的方式。