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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:34:08+00:00 2026-05-16T23:34:08+00:00

i was using object literal to create an object with methods. Here a simple

  • 0

i was using object literal to create an object with methods.
Here a simple example.

var SizeManager = {
    width : 800,
    height : 600,
    ratio : this.width / this.height,
    resize : function (newWidth) {
        width = newWidth;
        height = newWidth / ratio;
    }
}

My issue is that SizeManager.ratio returns “NaN“. I’m quite sure it’s an initialization issue.
Is there a way to obtain a correct ratio value?
Is there a way to assign a costructor or initializer to an object literal?
Is defining a constructor objcet the only way?

EDIT: off course SizeManager is ideally a singleton (only one object), that’s way i was using object literal.

  • 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-16T23:34:09+00:00Added an answer on May 16, 2026 at 11:34 pm

    Yes, it’s an initialization issue. this does not refer to your SizeManager object at the point you’re using it. (Object initializers don’t change the value of this.) this is set by how you call a function and has the same value throughout that function call. You’re not calling any function there, so this has whatever value it had prior to the beginning of that code.

    (I’ve pointed out something about ratio from your specific example at the very end of this, but first let’s walk through a few options for the general case you raise.)

    Daniel’s given you a good steer on making ratio a function except he doesn’t seem to have realized that you want to vary the width. Alternately, if width and height aren’t going to change, just calculate it afterward:

    var SizeManager = {
        width : 800,
        height : 600,
        resize : function (newWidth) {
            this.width = newWidth;
            this.height = newWidth / this.ratio;
        }
    };
    SizeManager.ratio = SizeManager.width / SizeManager.height;
    

    (Side note: I’ve added this. to the properties you’re referencing in resize. They were missing from your original, but they’re required. Without them, you’re dealing with the horror of implicit globals, which is a Bad Thing(tm).)

    Of course, you might encapsulate all of that into a factory:

    function makeSizeManager(width, height) {
        return {
            width : width,
            height : height,
            ratio : width / height,
            resize : function (newWidth) {
                this.width = newWidth;
                this.height = newWidth / this.ratio;
            }
        };
    }
    var SizeManager = makeSizeManager(800, 600);
    

    …but then you might as well make it an actual constructor function so you don’t create lots of duplicate (but identical) resize functions:

    function SizeManager(width, height) {
        this.width = width;
        this.height = height;
        this.ratio = width / height;
    }
    SizeManager.prototype.resize = function (newWidth) {
        this.width = newWidth;
        this.height = newWidth / this.ratio;
    };
    var aSizeManagerInstance = new SizeManager(800, 600);
    

    (Note I changed the names a bit on this last one.)

    And as one last final note: In your specific example, you don’t actually need to store ratio at all, you could do this:

    var SizeManager = {
        width : 800,
        height : 600,
        resize : function (newWidth) {
            var ratio = this.width / this.height;
            this.width = newWidth;
            this.height = newWidth / ratio;
        }
    };
    

    But that’s just for that specific example, hence the discussion above to talk about the general case.

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

Sidebar

Related Questions

I've been experimenting with using Javascript object literal notation vs functions with prototypes and
How do I access the name of an item in an Object Literal using
if in a literal object i try to reference a function using this inside
I am using the fluent interface to create a Zend DB Select object/query. As
I have the following object literal: var a = {a:1,b:2} now I want another
I'm creating an object using literal notation. Is it possible to have some of
I know you can create literal objects with subobjects and functions: var obj =
I'm trying to call a function within an object literal that I created, using
I am creating an object literal, which I'm using as a namespace. The object
What is the best way to build constructors in JavaScript using object literal notation?

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.