473,664 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.change Name("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 1675
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.change Name("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.change Name("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
10186
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 (not by assigning it to an
4
2850
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 class or namespace. Where xxx is the function being called. How do I call the function?
2
3018
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 or more user controls dynamically by adding to a placeholder defined in page_load. I've included the sample code for how we are accessing one. The user controls are not rocket science - just a few text boxes with public accessor properties. We've...
5
6531
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 new to VB.NET, so I need some help. Here is a code snippit :
2
4672
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 of the service is not set (this can be checked by right-clicking on the service in the Services window (Admin tools>Services), choosing Properties, LogOn tab). In order to enable my service to launch a GUI at startup, I added the following lines...
1
6111
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 PRINTER_DEFAULTS Dim pinfo2 As PRINTER_INFO_2 Dim pinfo8 As PRINTER_INFO_8 Dim pinfo9 As PRINTER_INFO_9 Dim dm As DEVMODEW Dim yDevModeData() As Byte
9
6388
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 = CType(e.Item.FindControl("edit_name"), TextBox)
46
3832
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 property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath....
3
6416
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 to be designed so that it will allow inheritance (no __class__ defined, etc). For example the Client object from pysvn. import pysvn class MyClient(pysvn._pysvn._Client): pass Traceback (most recent call last): File "<stdin>",...
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.