Solved

Question about JavaScript scoping

Posted on 2015-01-19
2
46 Views
Last Modified: 2015-01-19
My question is in the comments. Thanks.

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="UTF-8">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                $("#btn").click(function() {
                    var t = new TestConstructor();
                    t.sayHello();
                });
                
                function TestConstructor() {
                    this.sayHello = function() {
                        /*
                         * THIS FUNCTION IS NOT ACCESSIBLE. HOW TO ACCESS??
                         */
                        addAgain("Hello");
                    }; // END sayHello()
                    
                    /*
                     * THIS FUNCTIONS IS NOT ACCESSIBLE SOMEHOW. HOW CAN I
                     * ACCESS THIS FUNCTION INTERNALLY IN THIS CONSTRUCTOR
                     * FUNCTION? IT'S GIVING A MESSAGE SAYING addAgain IS
                     * NOT DEFINED. (click the Test button to see the error)
                     */
                    this.addAgain = function(str) {
                        console.log(str + " again!");
                    } // END addAgain
                    
                    /*
                     * I KNOW THIS FUNCTION WORKS, BUT IT'S NOT MY QUESTION.
                     */
                    function addAgain2(str) {
                        console.log(str + " again!");
                    } // END addAgain2()
                } // END TestConstructor()

            });
        </script>
    </head>
    <body>
        <input type="button" id="btn" value="Test" />
    </body>
</html>

Open in new window

0
Comment
Question by:elepil
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40558538
THIS FUNCTION IS NOT ACCESSIBLE. HOW TO ACCESS??
Create an "instance":

var testInstance = new TestConstructor();

testInstance.sayHello();

Open in new window


HOW CAN I ACCESS THIS FUNCTION INTERNALLY IN THIS CONSTRUCTOR FUNCTION?
You would ordinarily use the this qualifier, but since you are within a nested function, this will point to that particular function. You need to introduce a closure around the this that represents your constructor. The typical pattern is to introduce a variable named "self":

                function TestConstructor() {
                    var self = this;
                                        
                    self.sayHello = function() {

                        self.addAgain("Hello");

                    }; // END sayHello()
                    
                    self.addAgain = function(str) {

                        console.log(str + " again!");

                    } // END addAgain

                    ...
                    
                } // END TestConstructor()

Open in new window

0
 

Author Closing Comment

by:elepil
ID: 40558570
Excellent answer, kaufmed, thanks! (And thanks for that link on closure, it is one of the clearest articles on the subject I've ever read!)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now