473,574 Members | 2,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object creation on page load, default function and other public functions

Hi all,

I have created an object with a default function; the default function
exposes a public function as well.

myobject = function(){
var objects = [];
function addLoad(func){v ar oldonload = window.onload;i f(typeof
window.onload != 'function'){win dow.onload = func;}else{wind ow.onload =
function(){if(o ldonload){oldon load();}func(); }}}
function object(div){
/* some code here */
}
return function(div){
addLoad(functio n(){objects.pus h(new object(div))});
myobject.test = function(txt){a lert(txt);};
}
}();
object("mytextb ox");

How would I reference the object itself in such a way that I do not have to
use the object name?

Meaning; I'd like to get rid of:

myobject.test = function(txt){a lert(txt);};
And have it read something like:

this.test = function(txt){a lert(txt);};
(but that will add the function to the window object which is something I do
not want)

Or

var that = this;

that.test = function(txt){a lert(txt);};

(but that doesn't work also...)


My guess is you can't because the object is being created as the page loads
and there is no way to get its name until it's finished creating. (I don't
like to add the function(s) with prototype because the function should
operate as a singleton anyway and it... well... just looks wrong I guess)

Regards
Dec 11 '07 #1
2 1893
On Dec 11, 5:14 pm, "Calm_Pear" <sorry...@dirty mail.comwrote:
Hi all,

I have created an object with a default function; the default function
That statement is a bit confused. You have indeed created an object
(a function object.) It has no "default function", but does have two
inner functions, one of which will apparently be used as a
constructor. I suspect you want to use the outer "myobject" function
as a constructor, but it is hard to see why.
exposes a public function as well.

myobject = function(){
var objects = [];
function addLoad(func){v ar oldonload = window.onload;i f(typeof
window.onload != 'function'){win dow.onload = func;}else{wind ow.onload =
function(){if(o ldonload){oldon load();}func(); }}}
function object(div){
/* some code here */
}
return function(div){
addLoad(functio n(){objects.pus h(new object(div))});
myobject.test = function(txt){a lert(txt);};
}}();

object("mytextb ox");

How would I reference the object itself in such a way that I do not have to
use the object name?

Meaning; I'd like to get rid of:

myobject.test = function(txt){a lert(txt);};

And have it read something like:

this.test = function(txt){a lert(txt);};
Call it as a constructor, but the results won't be particularly
useful.

myStrangeObject = new object("mytextb ox");

BTW, constructors should have capitalized names. Unfortunately,
"Object" is taken.
(but that will add the function to the window object which is something I do
not want)

Or

var that = this;

that.test = function(txt){a lert(txt);};

(but that doesn't work also...)
The result is the same as with your previous attempt (a "test"
property is added to the global object.)
>
My guess is you can't because the object is being created as the page loads
Your theory has no basis in fact.
and there is no way to get its name until it's finished creating. (I don't
like to add the function(s) with prototype because the function should
operate as a singleton anyway and it... well... just looks wrong I guess)
You need to stop guessing. You should learn how objects work in
JavaScript before trying to create them. This example seems too
complex for a first attempt.
Dec 11 '07 #2
>>
>I have created an object with a default function; the default function

That statement is a bit confused. You have indeed created an object
(a function object.) It has no "default function", but does have two
inner functions, one of which will apparently be used as a
constructor. I suspect you want to use the outer "myobject" function
as a constructor, but it is hard to see why.
I'm sorry if my names are incorrect and caused confusion.

I want to attach an event to 1 or more text boxes and onkeyup update a div,
I stripped a lot of functionality in an attempt to focus on the issue.
I create an object for every text box to keep track of several things
including the div (and if the div doesn't exist create it).

So there are two private functions (The 'object' constructor [with the wrong
name 'object'] and the 'addLoad'), 1 public anonymous function that runs as
soon as myobject is being called and as this happens exposes 1 new public
function 'test'.

In my original code the private function 'object' is called 'o' by the
way...
>myobject = function(){
var objects = [];
function addLoad(func){v ar oldonload = window.onload;i f(typeof
window.onloa d != 'function'){win dow.onload = func;}else{wind ow.onload =
function(){if( oldonload){oldo nload();}func() ;}}}
function object(div){
/* some code here */
}
return function(div){
addLoad(functio n(){objects.pus h(new object(div))});
myobject.test = function(txt){a lert(txt);};
}}();

object("mytext box");

How would I reference the object itself in such a way that I do not have
to
use the object name?

Call it as a constructor, but the results won't be particularly
useful.

myStrangeObject = new object("mytextb ox");
Sorry again, that should have been:
myobject("textb ox");

>My guess is you can't because the object is being created as the page
loads
Your theory has no basis in fact.
Could you please explain that?
>
>and there is no way to get its name until it's finished creating. (I
don't
like to add the function(s) with prototype because the function should
operate as a singleton anyway and it... well... just looks wrong I guess)

You need to stop guessing. You should learn how objects work in
JavaScript before trying to create them. This example seems too
complex for a first attempt.
I can assure you this is not a first attempt and I have been reading up on
this subject. But I can understand you got that feeling because I'm not
particularly good when it comes to using the proper name for objects or
function objects. (Please also note that English is not my native language)
Dec 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2969
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the Intel compiler it has stopped working. I think the singleton pattern static instance thing may be at the root of the problem, but I'm not sure. I had...
6
7951
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a...
2
1133
by: FredC | last post by:
OS Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1 Microsoft Visual C# .NET 69462-335-0000007-18707 Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3
4
2795
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than one runat=server form on a asp.net page. However, I still want developers to be able to use asp.net controls to create some apps that are created...
11
3811
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
12
5531
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
4
2111
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
7
3573
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id - FirstName - LastName - CompanyId (many-to-one )
0
1103
markrawlingson
by: markrawlingson | last post by:
Hello, So I've got a list of functions written in ASP Classic which I include into every page within my application(s). This file is getting quite big and in most scenarios needs to be called 4-5 times for each page load because of the way I structure my pages (sitehead.asp,sitefoot.asp,sidemain.asp, etc). Thus, i'm trying to compile them as a...
0
7726
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...
0
8053
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. ...
0
8237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7817
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...
0
8098
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5301
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...
0
3752
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2245
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1060
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.