简体   繁体   中英

Get properties of an object from a function inside this object

Assume I have an object and an example of how I would like to get a property height from this object inside a function that is inside the object too:

function Chart(element) {
    return {
        width: element.clientWidth,
        height: 400,
        loadLegend: function (data) {
            doSomething(this.height)
        }
    }
};

This code doesn't work, as it's just an illustration to what I need to achieve. Please advise how I can get that property from the function?

function Chart(element) {
    var chart = {
        width: element.clientWidth,
        height: 400
    };
    chart.loadLegend = function (data) {
        doSomething(chart.height)
    };
    return chart;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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