473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1663
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jadow | last post by:
I have a function that is used as an event for some parts of the script. i.e. function somethind(e) { if (!e.currentTarget.documentElement ){ ... } } I need to call this function manually...
4
by: Dave | last post by:
I have a program that I've written a class for. I need to call the function in the program from the class. When I try to call the function I receive the error, the name xxx does not exist in the...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
2
by: letibal | last post by:
Hello, I have written a windows service and created an installer for it. The service runs under the system accounts. When started, it launches a GUI. By default, the InteractiveProcess property...
1
by: sathyp | last post by:
Public Function SetPrinterDefaultsW(ByVal sPrinterName As String, _ ByVal nPaperSize As Long, ByVal nOrientation As Long) As Boolean Dim Prn As Printer Dim hPrinter As Long Dim pd As...
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
3
by: MisterPete | last post by:
I've run into this issue a couple of times. I want to inherit from a class in order to extend it's functionality. The problem is that the class is defined in c in a shared library and doesn't seem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.