1

I've created a class "BFSPlayer" in Javascript with a constructor, and inside this constructor I've defined a function(which is not executed) named "myFunction". Then I've created an object "obj" of BFSPlayer class and then I want to run that myFunction for this object. Here's how I'm trying to do it, but it's not working:

class BFSPlayer {
  constructor(args){
    var player;
    function myFunction(){
        player = {
            videoId: args
        }
    }
  }
}

var obj=new BFSPlayer('video-id');
obj.constructor.myFunction();

Any help would be thankful to me

3
  • Function myFunction() is private to the constructor function. It is not a property of the constructed object and it is not a property of the constructor function itself. It is no more available outside the constructor than the variable "player" is. Commented Jan 7, 2023 at 15:14
  • 1
    Assign it to this inside a constructor. this.myFunction = function myFunction(){... Commented Jan 7, 2023 at 15:15
  • Thanks @vbykovsky your method is working great for me :), since you have commented and not posted an answer, therefore can't accept it as an answer. Thanks again Commented Jan 7, 2023 at 15:24

1 Answer 1

2

Example:

class BFSPlayer {
  player = {};

  constructor(args){
    this.player = {
      videoId: args,
    };
    
  }
  
  // I cant imageing what you wanna do here)
  //function myFunction(){
  //      player = {
  //          videoId: args
  //      }
  //  }
}

var obj = new BFSPlayer('video-id');

console.log(obj); //{ "player": { "videoId": "video-id" } }

// if you extract function from comment in class, you can use in this way
//obj.myFunction();

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

Comments

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.