473,473 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Create 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){var oldonload = window.onload;if(typeof
window.onload != 'function'){window.onload = func;}else{window.onload =
function(){if(oldonload){oldonload();}func();}}}
function object(div){
/* some code here */
}
return function(div){
addLoad(function(){objects.push(new object(div))});
myobject.test = function(txt){alert(txt);};
}
}();
object("mytextbox");

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){alert(txt);};
And have it read something like:

this.test = function(txt){alert(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){alert(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 1886
On Dec 11, 5:14 pm, "Calm_Pear" <sorry...@dirtymail.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){var oldonload = window.onload;if(typeof
window.onload != 'function'){window.onload = func;}else{window.onload =
function(){if(oldonload){oldonload();}func();}}}
function object(div){
/* some code here */
}
return function(div){
addLoad(function(){objects.push(new object(div))});
myobject.test = function(txt){alert(txt);};
}}();

object("mytextbox");

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){alert(txt);};

And have it read something like:

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

myStrangeObject = new object("mytextbox");

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){alert(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){var oldonload = window.onload;if(typeof
window.onload != 'function'){window.onload = func;}else{window.onload =
function(){if(oldonload){oldonload();}func();}} }
function object(div){
/* some code here */
}
return function(div){
addLoad(function(){objects.push(new object(div))});
myobject.test = function(txt){alert(txt);};
}}();

object("mytextbox");

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("mytextbox");
Sorry again, that should have been:
myobject("textbox");

>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
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...
6
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...
2
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...
4
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...
11
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...
12
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. ...
4
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
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 -...
0
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...
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
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...
1
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
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...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
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.