Declaring a member function in JS
I've tried two ways to declare a member function in JS: function init() { var name = "Mozilla"; function displayName() { alert(name); } } a = new init(); a.displayName() And function init() { var name = "Mozilla"; displayName = function() { alert(name); } } a = new init(); a.displayName() The first method told me that displayName() is undefined . The way I see it a variable of type Function with nae displayName is created, and thus it should work. Any one care to explain why it didn't work? Thanks To create something like a member function you need to add it to the protoype of the constructor