469,623 Members | 1,036 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

unable to call a function defined by an object

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?
Dec 3 '07 #1
2 1558
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();
Dec 3 '07 #2


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
Dec 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave | last post: by
2 posts views Thread by paul meaney | last post: by
9 posts views Thread by Jim in Arizona | last post: by
46 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by MisterPete | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.