views:

51

answers:

3

I'm on a .NET MVC2 project and have a reference to SomeClass.Home.js and jquery in the masterpage. My SomeClass.Home.js looks like this:

SomeClass.Home = {};

$(document).ready(function () {
    SomeClass.Home.SomeMethod();    
});

SomeClass.Home.SomeMethod= function () {
    alert("hello");
};

The call to SomeClass.Home.SomeMethod doesn't work (I don't get the alert). However, if I change it to this, it works, and I get the alert:

$(document).ready(function () {
    SomeMethod();    
});

function SomeMethod () {
    alert("hello");
};

Is anything wrong with the syntax of the first one?

+1  A: 

Yes, because you're not declaring the method. I believe you should do it like this:

SomeClass.Home = {
   SomeMethod = function(){ //stuff });
}

$(function(){ SomeClass.Home.SomeMethod() });
Jason
+1  A: 

What if you embed the function in the class?

SomeClass.Home = {
  SomeMethod= function () {
    alert("hello");
  };
};

$(document).ready(function () {
    SomeClass.Home.SomeMethod();    
});

SomeClass.Home.SomeMethod= function () {
    alert("hello");
};
J J
Also, why is this post tagged with C#?
J J
Removed the c# and .net tag. It was by habit.
Prabhu
+2  A: 

The problem seems to be in the way you described the SomeClass variable. The following code works for me.

var SomeClass = {};
SomeClass.Home = {};
SomeClass.Home.SomeMethod = function() {
  alert("hello");
};

$(document).ready(function () {
    SomeClass.Home.SomeMethod();    
});
calvinf
I wrote it like this too and it worked.
jtp
This was it, thanks.
Prabhu