javascript - OOP, private functions inside a top level method of the namespace - Stack Overflow

I have a js script which declares a namespace, then has a method called run() which I can call from ins

I have a js script which declares a namespace, then has a method called run() which I can call from inside a XUL script like myNamespace.run():

var myNamespace = {
    run: function() {
        var selectedText = getSelText();
        alert (selectedText);

        var getSelText = function() {
            var focusedWindow = documentmandDispatcher.focusedWindow;
            var selText = focusedWindow.getSelection();

            return selText.toString();
        }
    }
}

I want to be able to call getSelText() inside myNamespace.run() without needing to declare getSelText() as another top level function of myNamespace. Instead, it should be like a private method inside myNamespace.run().

When I run this script I receive an error:

getSelText is not a function.

I'm pretty new to JavaScript, so I don't know the best way of designing this. Is it possible to acheive what I am trying? Am I going about this the wrong way?

Appreciate any help!

I have a js script which declares a namespace, then has a method called run() which I can call from inside a XUL script like myNamespace.run():

var myNamespace = {
    run: function() {
        var selectedText = getSelText();
        alert (selectedText);

        var getSelText = function() {
            var focusedWindow = document.mandDispatcher.focusedWindow;
            var selText = focusedWindow.getSelection();

            return selText.toString();
        }
    }
}

I want to be able to call getSelText() inside myNamespace.run() without needing to declare getSelText() as another top level function of myNamespace. Instead, it should be like a private method inside myNamespace.run().

When I run this script I receive an error:

getSelText is not a function.

I'm pretty new to JavaScript, so I don't know the best way of designing this. Is it possible to acheive what I am trying? Am I going about this the wrong way?

Appreciate any help!

Share Improve this question edited Mar 30, 2012 at 18:36 FishBasketGordo 23.1k4 gold badges59 silver badges92 bronze badges asked Mar 24, 2012 at 17:13 bobbyrne01bobbyrne01 6,76322 gold badges84 silver badges168 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It's called the module pattern. Create an anonymous function (for scoping) that gets called immediately and return the public interface.

var myNamespace = (function() {
  // private functions for your namespace
  var getSelText = function() {
        ...
  };

  return {
    run: function() {
        var selectedText = getSelText();
        alert (selectedText);
    }
  };
})()

If you declare getSelText as a var, it needs to e before any statements where you use it. Just make the var getSelText = ... statement the first statement in the run function.


Supplemental information:

var statements in Javascript are "hoisted" to top of the function they're declared in, so this:

function() {
    var a = 42;
    // Other stuff...
    var b = 23; 
}

really means this:

function() {
    var a = 42, b;
    // Other stuff...
    b = 23;
}

Another similar construct in Javascript is the function statement, as opposed to function expressions:

function() {
    var fn1 = function() { return fn2(); }; // Function expression
    alert(fn1());

    // Function statement
    function fn2() {
        return "Hello from fn2";
    }
}

If you ran the code above, it would successfully alert "Hello from fn2". This is because function statements are hoisted to the tip-top before any other statement within the scope they're declared in. As you can see, this could get very confusing. Probably the best convention to follow is to use function expressions and declare them in the order that you need.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744372932a4571045.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信