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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:29:16+00:00 2026-06-15T12:29:16+00:00

Could you tell me why this will not work? (note the this keyword) var

  • 0

Could you tell me why this will not work? (note the this keyword)

var post = {
    url: 'http://myurl.com',
    add: function() {
        window.location = this.url + '/add';
    },
    edit: function() {
        window.location = this.url + '/edit';
    }
};

Somewhere else in the code:

post.url = '<?php echo BASE_ADMIN . $postType ?>';

$(document).ready(function() {

    $("#listing").aJqueryPlugin({
    ...
    // Buttons and their callbacks
    buttons : [
        {name: 'Add', bclass: 'add', onpress : post.add},
        {name: 'Edit', bclass: 'edit', onpress : post.edit},
    ],
   ...
});

The line

post.url = ….

behaves as expected. The url property in post is updated.

However, when I click on the Add or Edit buttons, and I enter their functions, this.url is undefined because this references the button instead of the post object. Why? What should I do then to reference the url property from a callback?

  • 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-15T12:29:18+00:00Added an answer on June 15, 2026 at 12:29 pm

    Since you’re using jQuery, you can use $.proxy.

        {name: 'Add', bclass: 'add', onpress : $.proxy(post, "add")},
        {name: 'Edit', bclass: 'edit', onpress : $.proxy(post, "edit")},
    

    This returns a new function that will call the method of the object named by the string.


    It’s effectively doing this:

    {name: 'Add', bclass: 'add', onpress : function() {
                                               return post["add"].apply(post, arguments);
                                           },
    {name: 'Edit', bclass: 'edit', onpress : function() {
                                               return post["edit"].apply(post, arguments);
                                             },
    

    Because the value of this in a function is dependent on how the function was called, you sometimes need alternate means to ensure that you get the correct value.


    You could also set these in your original object, as long as you know you always want this to refer to that object.

    var post = {
        url: 'http://myurl.com'
    };
    post.add = $.proxy(function() {
        window.location = this.url + '/add';
    }, post);
    post.edit = $.proxy(function() {
        window.location = this.url + '/edit';
    }, post);
    

    This uses the other signature of $.proxy, which lets you pass the function directly followed by the desired this value.

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

Sidebar

Related Questions

Could someone tell me if this: $.ajax({ url: 'test.html', success: function(data) { alert(Data Loaded:
Could someone tell me why this code does not update the data response? It's
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
could someone tell me if this is the right way to do bidirectional mapping?
I wonder why my regular expression will not work, I require to achieve the
In this document, http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#paths To make these device objects accessible by Windows applications, the
Ok so i was wondering if anyone could tell me where i am going
I was wondering if anyone could tell me about the i++ operator in C#.
I was wondering if someone could tell me the best way to call a
I was wondering if anyone could tell me if what I'm trying to accomplish

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.