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

  • Home
  • SEARCH
  • 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 6563045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:46:57+00:00 2026-05-25T13:46:57+00:00

I have been trying to write a constructor function that would make an object

  • 0

I have been trying to write a constructor function that would make an object chock full of complex methods. These methods process variables of their parents as-well as call on sibling methods. Here’s the best example I can give you. It’s for an improvised web-based console.

function ConsoleCont(type,instace){
    this.type=type;
    this.input=$('conI');//input type=text
    this.output=$('conO');//textarea
    this.commandRecall=new Array;
    //event listeners for enter-key to this.send(), etc.
    this.send=function(){
        if(this.input.value){this.commandRecall.push(this.input.value);}
        this.consoleConcat("> "+this.input.value);
        /*Code to send via AJAX*/
        this.input.value="";
    }
    this.consoleConcat=function(command){
        /*code to push to Textarea*/
    }
    this.receive=function(latestLine){
        this.consoleConcat(latestLine)
    }
}

Now I find myself needing to type “this.” for everything I reference within the object; is there anyway to have it assume ‘this.’ (like C++’s using namespace or something) or perhaps my methods for this a bit inefficient? I’m not looking for any Jquery solutions, I had predefined $(''); myself, incase you caught that.

  • 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-05-25T13:46:58+00:00Added an answer on May 25, 2026 at 1:46 pm

    You can ditch a few this‘s in your code, as far as I can see:

    function ConsoleCont(type,instace) {
    //                             ^did you mean instance?
        // both input/output are assigned to a fixed value, so they seem
        // not specific to an instance, hence can be ('static'-like) variables
        var input  = $('conI'), 
            output = $('conO'); 
    
        // these are really local (instance) properties
        this.type  = type;
        this.commandRecall = [];
    
        // the event listener(s) can be added to the constructors prototype
        if (!ConsoleCont.prototype.send){
         var proto = ConsoleCont.prototype;
         proto.send = function(){
            if(input.value){
               this.commandRecall.push(this.input.value);
            }
            consoleConcat("> "+this.input.value);
            /*Code to send via AJAX*/
            this.input.value = "";
         };
        }
    
        // if not used as public methods, these methods can be 
        // assigned and used within the current scope ('private')
        function consoleConcat(command){
            /*code to push to Textarea*/
        }
    
        function receive(latestLine){
            consoleConcat(latestLine)
        }
    }
    

    So, checking your code for the necessity of values or functions being properties can reduce the number of this‘s. The only other mechanism to reduce typing all this all over may indeed be using with, but that has some pitfalls, as Douglas Crockford explained here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to write an extend function so I can have bunch of
I have been trying to write some applescript that checks to see if the
I have been trying to make a case for using Python at my work.
I have been trying to learn how to write faster more efficient jQuery and
So I am trying to write a compiler in F# and have been looking
I have been trying to create a templated class( Test2 ) that takes 2
I've been trying to write a program for the last several hours that does
I have been trying to write Scheme code to get the date and store
In trying to write more testable Java code, I have been using the Model-View-Presenter
I have been trying to write to a file,but it kept writing them on

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.