调用对象时使用对象的成员而不是对象本身

时间:2019-07-30 18:18:32

标签: javascript

这是类和对象:

class BDImage
{
    constructor(path)
    {
        this.img = new Image();
        this.img.src = path;
    }
}

var myImage = new BDImage("path/to/image.png");

我想访问/使用myImage的{​​{1}}成员,而不必一直输入img。相反,只是myImage.img,然后我实际上是指myImage

如何?

3 个答案:

答案 0 :(得分:1)

只需将引用缓存在变量中:

class BDImage
{
    constructor(path)
    {
        this.img = new Image();
        this.img.src = path;
    }
}

var myImage = new BDImage("path/to/image.png");

var imgProp = myImage.img;  // Now imgProp points to myImage.img

console.log(imgProp.src);

答案 1 :(得分:1)

您似乎需要一些功能编程

var textFields = <Widget>[];

希望有帮助!

答案 2 :(得分:1)

扩展;)

mysql> SELECT
    QUARTER ('2019-01-01') AS `1th q`,
    QUARTER ('2019-04-30') AS `1th q`,
    QUARTER ('2019-05-01') AS `2th q`,
    QUARTER ('2019-08-31') AS `2th q`,
    QUARTER ('2019-09-01') AS `3th q`,
    QUARTER ('2019-12-31') AS `3th q`;
+-------+-------+-------+-------+-------+-------+
| 1th q | 1th q | 2th q | 2th q | 3th q | 3th q |
+-------+-------+-------+-------+-------+-------+
|     1 |     2 |     2 |     3 |     3 |     4 |
+-------+-------+-------+-------+-------+-------+
1 row in set

mysql> 

Object.defineProperty

+-------+-------+-------+-------+-------+-------+
| 1th q | 1th q | 2th q | 2th q | 3th q | 3th q |
+-------+-------+-------+-------+-------+-------+
|     1 |     1 |     2 |     2 |     3 |     3 |
+-------+-------+-------+-------+-------+-------+