Solved

Javascript function scoping question

Posted on 2015-01-19
5
62 Views
Last Modified: 2015-01-20
Please refer to code sample below. 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() {
                        addAgain("Hello");
                    };
                    
                    this.addAgain = function(str) {
                        console.log(str + " again!");
                    }
                    
                    function addAgain2(str) {
                        console.log(str + " again!");
                    }
                }

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

Open in new window

0
Comment
Question by:elepil
  • 3
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40558188
My question is in the comments.
Are you sure?
0
 

Author Comment

by:elepil
ID: 40558480
Oh sorry. I apparently had put my comments in my editor and forgot to copy/paste it back into the post. Here is the code again with the comments.

<!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
 

Author Comment

by:elepil
ID: 40560379
I've requested that this question be deleted for the following reason:

Reposting
0
 
LVL 5

Accepted Solution

by:
GuruJava earned 500 total points
ID: 40559421
hi elepil,

Please put "this" in your code before that function name.... that will work ...


<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="UTF-8">
        <script src="D:/Users/t7091/Desktop/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??
                         */
                        this.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
 

Author Closing Comment

by:elepil
ID: 40560380
Thanks, GuruJava!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
updateParent() HTML Javascript 21 61
Problem to picture file 3 40
jQuery animation faster 1 17
getting id from database 5 24
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now