简体   繁体   English

对象返回自己的属性并保留方法

[英]Object returns own property and preserves methods

I have an object that wraps some data: 我有一个包装一些数据的对象:

function Obj1() {
    var _foo = 'bar'
    this.obj2 = {
        'b': 'c'
    }
    this.method = function() {
        return _foo
    }
}
var obj1 = new Obj1()

Now when I call console.log(obj1); 现在,当我调用console.log(obj1); I want it to show me object obj2 content. 我希望它向我显示对象obj2内容。 The trick is that I need to still be able to call obj1.method and get value of _foo . 诀窍是我仍然需要能够调用obj1.method并获取_foo值。 How do I do that if it's even possible? 如果可能的话我该怎么办? My thought was that sth like getter will be suitable, but can't figure out where and how to assign one. 我的想法是,像getter这样的东西将是合适的,但无法弄清楚在哪里分配方法。

if i understand correctly, you can use prototype 如果我理解正确,可以使用prototype

Example

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())

As far as I understood you're trying to hide method property. 据我了解,您正在尝试隐藏method属性。 To achieve this, use Object.defineProperty . 为此,请使用Object.defineProperty Function will not be logged because enumerable property is false by default which prevents property from showing in console.log for example. 由于默认情况下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()); 

Since you calling new Obj1() . 自从您调用new Obj1()以来。 The result variable var obj1 is a class object and not a function, for you to get the value of obj2 you will have to call obj1.obj2 in your console log. 结果变量var obj1是一个类对象,而不是一个函数,要获取obj2的值,必须在控制台日志中调用obj1.obj2 If you want obj1 to hold the value of obj2. 如果要让obj1保留obj2的值。 Then use the following code 然后使用以下代码

function Obj1() {
    var obj2 = {
       'b': 'c'
    }
    return this.obj2;
}
var obj1 = Obj1();
console.log(obj1);

This will give you the required result in the console log, but the object will no longer be a class object and will have only the value of obj2. 这将在控制台日志中为您提供所需的结果,但是该对象将不再是类对象,而仅具有obj2的值。

Sticking to your original snippet a factory looks like a good option: 坚持使用原始代码段是一个不错的选择:

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'

You could even pass props as an argument to get a more flexible way of spawning objects with an " unenumerable " method accessing private members. 您甚至可以将props作为参数传递,以使用“ 无数 ”方法访问私有成员,以更灵活地生成对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM