1

I am trying to set a prototype function dynamically using new Function(...). I have tried the following (es6):

export default class AI {

    constructor(algObj, player) {
        this.player = player;
        this.algObj = algObj;

        //create the shoot and placeShips prototypes form the this.algObj property
        this.prototype.initialize   = new Function(this.algObj.initialize);
        this.prototype.shoot        = new Function(this.algObj.shoot);
        this.prototype.placeShips   = new Function(this.algObj.placeShips);

        this.initialize();
    }
}

USE CASE: I have a micro service that stores algorithms as the resource which will then be passed into a simulator that battles 2 algorithms.

when I try this, this.prototype is undefined. The only reason I can think this might be the case is because the AI object is not fully defined until after the constructor is done executing.

How would I go about setting a prototype function like I am trying to do here?

UPDATE:

this.__proto__.initialize   = new Function(this.algObj.initialize);
this.__proto__.shoot        = new Function(this.algObj.shoot);
this.__proto__.placeShips   = new Function(this.algObj.placeShips);
7
  • Are you planning to use AI for one object instance only? Commented Jun 12, 2016 at 17:15
  • I added a use case to my edit as well as an update. Commented Jun 12, 2016 at 17:18
  • There will by one AI instance per game and there will be multiple games per simulation. Commented Jun 12, 2016 at 17:19
  • You now have two questions: about prototype being undefined, and about assigned functions which are anonymous. Please don't mix these into one question: ask a separate one for each. Commented Jun 12, 2016 at 17:21
  • I will open another question for the anonymous function question Commented Jun 12, 2016 at 17:27

1 Answer 1

3

When the constructor is invoked you already have an instance of the object you're creating, and thus you can simply modify the instance's methods without touching the prototype:

export default class AI {

    constructor(algObj, player) {
        this.player = player;
        this.algObj = algObj;

        //create the shoot and placeShips prototypes form the this.algObj property
        this.initialize   = new Function(this.algObj.initialize);
        this.shoot        = new Function(this.algObj.shoot);
        this.placeShips   = new Function(this.algObj.placeShips);

        this.initialize();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This works! So are the 3 functions then added to AI.prototype
@Frank No, they are added to the instance the constructor is, well, constructing. The prototype remains untouched.
@jfriend00: Apparently algObj contains code strings, otherwise it wouldn't work. Why it does that, and whether that is a good idea, we better discuss elsewhere.
Yea I use new Function because I'm passing in a string.

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.