1

I have problem in line #27 (fiddle: https://jsfiddle.net/hh82p4fj/1/)

Why my property points to undefined while it's in the same closure?

"use strict";

function callself(instance, callback) { // saves 'this' state for reuse inside another function
    return function () {
        var argumentsList = [this], i;

        for (i = 0; i < arguments.length; i = i + 1) {
            argumentsList.push(arguments[i]);
        }

        return callback.apply(instance, argumentsList);
    };
}

var Namespace = function () {
    var settings = { handler: this.method };

    this.initialize = function () {
        $.fn.myPlugin = callself(this, function (that, userInput) {
            /* this = Namespace
               that = jQuery element
               userInput = provided in line 30 as parameter
            */
            console.log(this);
            console.log(that);
            console.log(userInput);
            console.log(settings.handler); // why why it's undefined?
        });

        $('body').myPlugin('someData');
    };

    this.method = function () { console.info('method'); }; // this method should be returned as reference in line 27, but it can not reah 'this` - don't know why
};
new Namespace().initialize();

callself is needed there, to preserve both jQuery this context ando ryginal object closure.

4
  • Check this jsfiddle.net/tusharj/hh82p4fj/2 Commented Jun 15, 2015 at 9:23
  • place this.method = function () {... before var settings= beacuse this.method is undefined when you defining settings Commented Jun 15, 2015 at 9:25
  • @Tushar your fiddle still returns undefined there :-) Commented Jun 15, 2015 at 9:34
  • @Ultra fiddle is not mine :D mine comment is correct. tested. Commented Jun 15, 2015 at 9:35

2 Answers 2

4

The problem is the order of initialization.

You are using this.method at the be genning of the script when this.method = function(){} is not yet executed so the value of this.method is undefined when the setting object is created.

var Namespace = function () {
    //this should be set before `this.method` is referred
    this.method = function () {
        console.info('method');
    };

    var settings = {
        handler: this.method
    };

    this.initialize = function () {
        $.fn.myPlugin = callself(this, function (that, userInput) {
            /* this = Namespace
               that = jQuery element
               userInput = provided in line 30 as parameter
            */
            console.log(this);
            console.log(that);
            console.log(userInput);
            console.log(settings.handler); // why why it's undefined?
        });

        $('body').myPlugin('someData');
    };

};
Sign up to request clarification or add additional context in comments.

1 Comment

I thought am passing a reference filled when this.initialize is executed (where it's existing).
1
var settings = { handler: this.method };

You assigned to settings.handler not a reference to this.method
but value of this.method (which is undefined at execution moment)

this.method must be an object to get referenced.


this.method = {}; //empty object
var settings = { handler: this.method };

this.method =>ref {}
settings.handler =>ref {}


this.method = {}; //empty object
var settings = { handler: this.method };
this.method = funtion(){}`// reference will be replaced

this.method =>ref funtion(){}
settings.handler =>ref {}


this.method = {}; //empty object
var settings = { handler: this.method };
this.method.use = function(){}

this.method =>ref {use: function(){}}
settings.handler =>ref {use: function(){}}


function Namespace(){
  
    var settings = {};

    this.initialize = function(){
        document.write(settings.handler())
    };

    settings.handler = function(){return 'Handled!'};
}
new Namespace().initialize()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.