?
Solved

Question about JavaScript scoping

Posted on 2015-01-19
2
Medium Priority
?
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

762 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