473,405 Members | 2,334 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,405 software developers and data experts.

How to create objects inside objects?

Hi

I have a javascript "class" that generates some html and handle some events and so. I want to have a "sub class" (an instance of an object) that is a child of the first one. The problem is that I can't access the parent object from the seccond one. Is something like this:

Expand|Select|Wrap|Line Numbers
  1. function B()
  2. {
  3.  
  4.    this.send_alert = function() { alert(this.parent); }
  5. }
  6. function A()
  7. {
  8.    var _a = 5;
  9.    var _b = new B();
  10.    _b.send_alert();
  11. }
That doesn't work. Any suggestions please?
Thanks
Jul 25 '07 #1
1 1096
gits
5,390 Expert Mod 4TB
Hi,

it works the way you did (i added an instance of A) ... but be aware with the this-keyword. when using objects this refers to the actual object-context so your alert will show 'B-parent' ... ok?

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3. function B() {
  4.     this.parent = 'B-Parent';
  5.     this.send_alert = function() { alert(this.parent); }
  6. }
  7.  
  8. function A() {
  9.     this.parent = 'A-Parent';
  10.  
  11.     var _a = 5;
  12.     var _b = new B();
  13.  
  14.     _b.send_alert();
  15. }
  16.  
  17. var a = new A;
  18.  
  19. </script>
to get the 'A-Parent' you have to use the call-method that calls the method within the execution-context that you pass as a parameter:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3. function B() {
  4.     this.parent = 'B-Parent';
  5.     this.send_alert = function() { alert(this.parent); }
  6. }
  7.  
  8. function A() {
  9.     this.parent = 'A-Parent';
  10.  
  11.     var _a = 5;
  12.     var _b = new B();
  13.  
  14.     _b.send_alert.call(this);
  15. }
  16.  
  17. var a = new A;
  18.  
  19. </script>
  20.  
kind regards
Jul 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to...
2
by: Robin Tucker | last post by:
I have some code that dynamically creates a database (name is @FullName) and then creates a table within that database. Is it possible to wrap these things into a transaction such that if any one...
2
by: Keith Jakobs, MCP | last post by:
Greetings: I had seen several posts around the Internet about this issue from a few months back, but have been unable to find a resolution ... I have a user who is trying to get started with...
10
by: Lars Grøtteland | last post by:
Hello! Was wondering how I can create a copy of one database? FYI I'm not interrested in the data, just the fields and triggers, and so on. Which way is the easiest? Do I create a script and...
2
by: ruca | last post by:
Hi people, I have a .vb file and I want to create a .dll file. I'm puting this line in command line: vbc /t:library /r:system.dll,system.web.dll,system.data.dll,microsoft.visualbasic.dll...
6
by: Rob Williamson | last post by:
Does anyone know whether there is a way to create folders inside the right payne when you click on Tables or Queries or Forms or Reports in the the Objects side. I have alot of queries and forms...
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
3
by: Alex | last post by:
Hi everyone, I have a question that implies thinking about some design issues with C ++. I have a method like this: class C { .... public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.