Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:24:00+00:00 2026-06-11T16:24:00+00:00

So I’m trying my hand at JavaScript classes to try and clarify and simplify

  • 0

So I’m trying my hand at JavaScript “classes” to try and clarify and simplify some of my code. What I have is something like this:

function action (name, vActor, vTarget) {
    this.name = name;
    this.vActor = vActor;
    this.vTarget = vTarget;
    this.addRoll = addRoll;
    this.children = {};
}
function addRoll (name, diffMod, dice, success, critSuccess, failure) {
    this.children[name] = {} ;
    this.children[name].diffMod = diffMod;
    this.children[name].dice = dice;
    this.children[name].success =       {condition: success,        outcome: {}};
    this.children[name].critSuccess =   {condition: critSuccess,    outcome: {}};
    this.children[name].failure =       {condition: failure,        outcome: {}};
    this.children[name].addSuccess = addSuccess;
    this.children[name].addFailure = addFailure;
    this.children[name].addOutcome = addOutcome;
}

Is this the right way to go about this? My main question is regarding who owns the “this” object in the “function addRoll()” section. I’m assuming that “this” still belongs to the action ‘class’. I also am uncertain of the syntax regarding starting a new blank object and assigning stuff using dot notation. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:24:01+00:00Added an answer on June 11, 2026 at 4:24 pm

    Ignoring function binding and calling apply or call, the this property is the owner of the method. Calling…

    addRole(...)
    

    The this property points to the global window object. If you had an object {} or instance new Something() called x with the function addRole and called it…

    x.addRole(...)
    

    The this property is x.

    Additional You have correctly assigned the function to the action object so when you call…

    var a = new action(...);
    

    And then call

    a.addRole(...);
    

    The this property is the a instance of action you have created. To prevent it being called as a global function and adding properties to the window you could assign it to a prototype. The prototype object has some powerful features to build inheritance but for now simply changing…

    addRole(...) {...}
    

    To the following…

    action.prototype.addRole = function(...) {...}
    

    And removing the assignment in action…

    this.addRole = addRole
    

    Will prevent the function accidentally being called without an owner

    Further You could rewrite the way you assign the children in addRole to make greater use of object literal notation…

    function addRoll(name, diffMod, dice, success, critSuccess, failure) {
        this.children[name] = {
            diffMod: diffMod,
            dice: dice,
            success: {
                condition: success,
                outcome: {}
            },
            critSuccess: {
                condition: critSuccess,
                outcome: {}
            },
            failure: {
                condition: failure,
                outcome: {}
            },
            addSuccess: addSuccess,
            addFailure: addFailure,
            addOutcome: addOutcome
        };
    }
    

    You could also refactor the code to use classes for the children as follows.

    function Action(name, vActor, vTarget) {
        this.name = name;
        this.vActor = vActor;
        this.vTarget = vTarget;
        this.children = {};
    }
    Action.prototype.addRoll = function(name, role) {
        this.children[name] = role;
    }
    
    function Role(diffMod, dice, success, critSuccess, failure) {
        this.diffMod = diffMod;
        this.dice = dice;
        this.success = success;
        this.critSuccess = critSuccess;
        this.failure = failure;
    }
    Role.prototype.addSuccess = function(...) {...}
    Role.prototype.addFailure = function(...) {...}
    Role.prototype.addOutcome = function(...) {...}
    
    function Condition(condition) {
        this.condition = condition;
        this.outcome = {};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.