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

The Archive Base Latest Questions

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

Possible Duplicate: “this” inside object I’m trying to make an object literal for a

  • 0

Possible Duplicate:
“this” inside object

I’m trying to make an object literal for a couple of default options for a jQuery plugin that I’m working on:

  var defaults = {

            r: 5,
            top: this.r,
            bottom: this.r,
            topleft: this.top,
            topright: this.top,
            bottomleft: this.bottom,
            bottomright: this.bottom


        };

when I reference the defaults.top it is undefined

anything I can do to make this work? Or perhaps a different approach? I need it to be an object literal.

Added:

It is (default object), the way it’s cascading down as you can see, was intended to be some what of a short hand technique. For example, if you would like to define all corners to be the same, you would use {r: 5} but if you want the top and bottom to be different {top: 5, bottom: 1} again, individually {topleft: 5, topright:2, bottomleft: 3, bottomright:19 } I apologize for not making this clear, but am very grateful for your answers.

ANSWERED: This is what I ended up doing

if(o.topleft == undefined || o.topright == undefined || o.bottomleft == undefined || o.bottomright == undefined){
                if(o.top == undefined || o.bottom == undefined){
                    if(o.r == undefined){
                        o.topleft = 5;
                        o.topright = 5;
                        o.bottomleft = 5;
                        o.bottomright = 5;
                    }else{
                        o.topleft = o.r;
                        o.topright = o.r;
                        o.bottomleft = o.r;
                        o.bottomright = o.r;  
                    }
                }
                else{
                    o.topleft = o.top;
                    o.topright = o.top;
                    o.bottomleft = o.bottom;
                    o.bottomright = o.bottom;
                }
            }

supper sloppy, but hey it worked! Thank you for all your help! I chose the answer because that explanation led me to do it this way!

  • 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-09T13:41:15+00:00Added an answer on June 9, 2026 at 1:41 pm

    “when I reference the defaults.top it is undefined“

    That’s because this doesn’t refer to the object you’re creating, it is the this from whatever scope that code is running in.

    Object literal syntax does not allow you to set values by referencing other properties in the same object – the object doesn’t exist yet. You can reference other variables or functions declared before the object literal. So if you need all the properties to be the same like in your example then you can do this:

    var val = 5,
        defaults = {
                r: val,
                top: val,
                bottom: val,
                topleft: val,
                topright: val,
                bottomleft: val,
                bottomright: val
        };
    

    Or create some of the properties with an object literal and set the rest afterwards:

    var defaults = {
            r : 5
        };
    
    defaults.top = defaults.bottom = defaults.r;
    defaults.topleft = defaults.topright = defaults.top;
    // etc
    

    Obviously the latter is more suited to setting some properties to one value and other properties to another value. (Though again in your example all properties are the same.)

    Either way gives you the same object in the end (an object literal is just a shortcut way to create objects).

    ” I would like it to be simple enough to do something like this $(selector).myPlugin({r:10}); or $(selector).myPlugin({top:10, bottom: 5}); “

    Well you can still call the plugin with an object literal as a parameter. But the defaults object (which I assume is defined inside the plugin) can be defined using other techniques.

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

Sidebar

Related Questions

Possible Duplicate: How to select an element by class inside “this” in Jquery I'm
Possible Duplicate: JavaScript losing “this” object reference with private/public properties Why does the second
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm trying to create
Possible Duplicate: setTimeout and “this” in JavaScript I am trying to put a timeout
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Edit: This has nothing
Possible Duplicate: What does this “(function(){});”, a function inside brackets, mean in javascript? javascript
Possible Duplicate: Is it OK to use “delete this” to delete the current object?
Possible Duplicate: What does this “(function(){});”, a function inside brackets, mean in javascript ?
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument This is very odd,
Possible Duplicate: PHP: “Notice: Undefined variable” and “Notice: Undefined index” This code shows warnings

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.