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 8754039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:32:32+00:00 2026-06-13T13:32:32+00:00

So I am trying to convert my web app over to being OOP-based since

  • 0

So I am trying to convert my web app over to being OOP-based since that’s how I learned to program in the first place. I have all my functions and everything defined, but I’m running into a problem.

Let’s say in the index.php I open a script tag and create an object function:

<script type="text/javascript">
function myObject(_string){
   this.string = _string;

   this.get_string = function(){
       return this.string;
   }
}
</script>

No biggie.

Now if I call it, it works fine if I do this:

var my_object = new myObject("this is a string");
console.log(my_object.get_string) // logs "this is a string"

but if I wrap it in a domReady, the object never gets created, and calling my_object returns a reference error:

$(document).ready(function() {
     var my_object = new myObject("this is a string");
     console.log(my_object); // returns reference error
}); 

I get this same problem if I embed a function inside my object and try to call it:

<script type="text/javascript">
    function myObject(_string){
       this.string = _string;

       this.get_string = function(){
           return this.string;
       }

       this.set_string = function(new_string){
            this.string = new_string;
       }
    }

    my_object = new myObject("this is a string");
    my_object.set_string("this is a new string"); // returns reference error
    my_object.set_string() // Returns reference error
    my_object.set_string // returns a string containing the function
</script>

Seriously confused over this. Can anyone help?

  • 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-13T13:32:33+00:00Added an answer on June 13, 2026 at 1:32 pm

    This should work regardless of where your code is placed

    function myObject(_string){
       this.string = _string;
    
       this.get_string = function(){
           return this.string;
       };
    
       this.set_string = function(new_string){
            this.string = new_string;
       };
    }
    

    And call the like:

    var my_object = new myObject("this is a string");
    console.log(my_object.get_string()) // will log "this is a string"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to convert a web project that produces a war file
I have a simple web service method which I'm trying to convert: <WebMethod()> _
Hey guys I am having trouble trying to convert my web app to support
I'm trying to build a simple web app that calculates either a male or
I have a web application that I am trying to port to Facebook. The
I am currently trying to complete a transaction for a web based app, however;
I am trying to convert a poorly developed C# web app to MVC2, and
I am new to ASP.NET and am trying to convert a web application from
i'm new to mod_rewrite, and i'm trying to convert my web address from: website.com/profile.php?user=andy
I'm trying to convert an ASP.NET web application to use resource files. I haven't

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.