Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > unable to call a function defined by an object

Reply
Thread Tools

unable to call a function defined by an object

 
 
yawnmoth
Guest
Posts: n/a
 
      12-03-2007
Say I have the following:

<script>
var cat = (function() {
var name = "";

function changeName(name) {
this.name = name;
}

function talk() {
alert( this.name + " say meeow!" );
}

return function(name) {
this.name = name;
talk();
}
})();

firstCat = new cat("pursur");
//firstCat.changeName("Bill");
//firstCat.talk(); //alerts "Bill says meeow!"
</script>

Why can I call the talk() function, as defined in the cat variable /
function, from the return function (eg. privately) but not publically?

Also, setting this.name to name doesn't seem to accomplish anything.
What shared variable could I set for talk() to print out the same
value that the return function set?
 
Reply With Quote
 
 
 
 
taps128
Guest
Posts: n/a
 
      12-03-2007
yawnmoth wrote:
> Say I have the following:
>
> <script>
> var cat = (function() {
> var name = "";
>
> function changeName(name) {
> this.name = name;
> }
>
> function talk() {
> alert( this.name + " say meeow!" );
> }
>
> return function(name) {
> this.name = name;
> talk();
> }
> })();
>
> firstCat = new cat("pursur");
> //firstCat.changeName("Bill");
> //firstCat.talk(); //alerts "Bill says meeow!"
> </script>
>
> Why can I call the talk() function, as defined in the cat variable /
> function, from the return function (eg. privately) but not publically?
>
> Also, setting this.name to name doesn't seem to accomplish anything.
> What shared variable could I set for talk() to print out the same
> value that the return function set?


Why don't you try
function Cat(){
....
}

var firstCat=new Cat();
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      12-03-2007


yawnmoth wrote:
> Say I have the following:
>
> <script>
> var cat = (function() {
> var name = "";
>
> function changeName(name) {
> this.name = name;
> }
>
> function talk() {
> alert( this.name + " say meeow!" );
> }
>
> return function(name) {
> this.name = name;
> talk();
> }
> })();
>
> firstCat = new cat("pursur");
> //firstCat.changeName("Bill");
> //firstCat.talk(); //alerts "Bill says meeow!"
> </script>
>
> Why can I call the talk() function, as defined in the cat variable /
> function, from the return function (eg. privately) but not publically?
>
> Also, setting this.name to name doesn't seem to accomplish anything.
> What shared variable could I set for talk() to print out the same
> value that the return function set?


I think the simplest answer is to ask you to read about closures:

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >

You may also like this article on implementing public and private
members in javascript:

<URL: http://www.crockford.com/javascript/private.html >

--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
constants defined in Kernel are also defined in Object? Paul Brannan Ruby 13 03-04-2008 03:09 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments