0

Here's the class and object:

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

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

I want to access/use myImage's img member without typing myImage.img all the time. Instead just myImage and then I'm actually referring to myImage.img.

How?

4
  • 1
    The language provides no facility to make that possible. You can always make another variable, or re-assign myImage to myImage.img. Commented Jul 30, 2019 at 18:19
  • You can declare another variable within the context in which it'll be used so that you can have var img = myImage.img; Commented Jul 30, 2019 at 18:20
  • @Pointy check developer.mozilla.org/de/docs/Web/JavaScript/Reference/… - its possible ;) Commented Jul 30, 2019 at 18:45
  • @Estradiaz that's a nice trick but it's not what the OP asked; I agree that it's what the OP can do to satisfy the original desire, but it's not a lot different from having a separate variable. Commented Jul 30, 2019 at 18:46

3 Answers 3

1

extends ;)

    class BDImage extends Image
    {
        constructor(path)
        {
            super()
            this.src = path
        }
    }

    var myImage = new BDImage("path/to/image.png");
    console.log(myImage, myImage.src);

Object.defineProperty

class BDImage
{
    constructor(path)
    {
        this.image = new Image()
        this.image.src = path
        Object.defineProperty(this, 'src', {
          get(){
            return this.image.src;
          },
          set(path){
            this.image.src = path
          }
       })
    }
}
myImage = new BDImage("path/to/image.png");
console.log(myImage, myImage.src);

myImage.image.src = "path/to/image2.png"
console.log(myImage, myImage.src);

myImage.src = "path/to/image3.png"
console.log(myImage, myImage.src);

Sign up to request clarification or add additional context in comments.

1 Comment

extends is just what I need. Thanks a bunch!
1

Just cache the reference in a variable:

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

Comments

1

It looks like you need some Functional programming !

function BDImage(path) {
    const img = new Image();
    img.src = path; 
    return img;
}; 

const myImage = BDImage("path/img.png");

Hope it helps !

4 Comments

How does this address the question that was asked?
It is exactly the result that was asked in an elegant alternative, even if it's using a function instead of a class.
Uh, no it's not. The question was how to be able to access myImage.img without having to type that and instead typing something simpler, like: myImage. The question had nothing to do with functions. "I want to access/use myImage's img member without typing myImage.img all the time. Instead just myImage and then I'm actually using myImage.img." In your code, you'd still have to write myImage.src.
Yeah, he write myImage.src instead of myImage.img.src as the function is directly return the img reference. Which is exactly what every other answers in the thread is doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.