0

i want to create some javascript object with few functions, but i get a exception

undefined is not a function

JS:

 var Buses = null;
 $(function () {
        Buses = function() {
            return {
                Test: function () {
                    console.log('test public function');
                }
            }
        }
 });

HTMl:

<button onclick="Buses.Test()">test</button>
<script type="text/javascript" src="js/buses.js" ></script>

What is wrong?

0

1 Answer 1

5

Your Buses variable is a function that returns a function so in the HTML it must be called like this

<button onclick="Buses().Test()">test</button>

While you are using jQuery, a better solution would be to give the button an and then assign the click handler right in the javascript:

var Buses = null;
$(function () {
    Buses = function() {
        return {
            Test: function () {
                alert('test public function');
            }
        }
    }

    $("#button").click(function() {Buses().Test()});
});

Here's the fiddle http://jsfiddle.net/fe5hxo60/

Sign up to request clarification or add additional context in comments.

1 Comment

he's be better yet creating a single instance of Buses and referring to that in the click handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.