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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:55:53+00:00 2026-06-07T03:55:53+00:00

var foo = { p1: function(){ return this.p2; }, p2: function(){ console.log(‘i am foo.p2’);

  • 0
var foo = {
  p1: function(){
    return this.p2;
  },

  p2: function(){
    console.log('i am foo.p2');
  }
};

I’m trying to do something similar to the example above, however I’m running into an issue where when I invoke:

var result = foo.p1();

result == ‘undefined’

I’m confused about the way ‘this’ works within object context. Could someone explain where I’m going wrong here?

EDIT
more complete example:

suite_segments.themis = {

    //don't re-run themis initialization script
    initialized: false,

    /**
     * Initializer for themis product. Returns true if initialization
     * operations were performed, false if not (most likely because 
     * the product was already initialized -- not a fresh navigation)
     */
    init: function(){

            //prevent multiple initializations
            if(this.initialized)
                return false; //did not initialize
            this.initialized = true;

            //operations
            jQuery('#tabs').tabs();


            //init success
            return this.themis_destroy;         
    },





    /* ----------------------------------------------------------------------------------
     *      DESTRUCTORS
     * ----------------------------------------------------------------------------------/
    /**
     * Function to be invoked if user navigates away from 'themis' entirely. Other
     * sub-destroy type functions will be invoked if necessary when a user switches 
     * between parts of themis
     * 
     */
    themis_destroy: function(){

        console.log('themis_destructor');
        this.initialized = false;

    },
    /**
     * Designed to be overwritten every time a segment of themis is loaded. Will be invoked 
     * ever time a segment of themis is loaded.
     */
    themis_sub_destroy: function(){}


};
  • 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-07T03:55:54+00:00Added an answer on June 7, 2026 at 3:55 am

    Your completed example will work as well. With your code, suite_segments.themis.init() will return the descructor function (or false), not undefined.

    But you have an other problem: the destructor won’t work. Read this excellent overview about the this keyword and you’ll see: this points to the current context, which is call-dependent. When invoked per ...themis.init(), the function will be called in the context of the themis object – everything is fine. But the function that is returned (suite_segments.themis.destroy) won’t be called on the object, but (I guess) standalone – and has no chance to set the initialized property of the correct object.

    In your case, I can recommmend the .bind() method to set the context of the returned function:

    return this.themis_destroy.bind(this);
    

    See also this blog post about “objects with properties that are functions” or the mythof of methods, which covers exactly your title question, and this post about this.

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

Sidebar

Related Questions

When I run this code: function foo() { console.log(foo); var self = this; this.a
I'm trying something like this: (function() { var Foo, foo; Foo = function(proto) {
var foo = { x: 1, y: (function () { return ++this.x; })() };
In this code: <?php class Foo { var $value; function foo($value) { $this->setValue($value); }
var f = function(o){ return this+:+o+::+(typeof this)+:+(typeof o) }; f.call( 2, 2 ); //
var foo = (function(){ var x = 0; return function(){return x++;}; })() Why the
var a = (function(y,x) { var x = null; return { foo: function(x){ return
function Foo(){ } Foo.prototype={ method1:function(o){ console.log(o); } ,shorthand:function(){ if(!arguments.length || typeof arguments[0]=='undefined') { arguments[0]={};
Help, I have this class var jMath = { pi2: Math.PI, foo: function() {
Lets imagine function like this: function foo(x) { x += '+'; return x; }

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.