Solved

Question about JavaScript scoping

Posted on 2015-01-19
2
48 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 75

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery 10 20
Checkout Page Input Field not aligned 1 23
Jquery-Javascript setAttribute function help 7 11
how can i make 2 columns? 4 22
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

803 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