473,809 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript object and lost property

Hi all. I have situation that when my page is loaded i create js object
<html>
...
<script>
function Page() {
this.page = 0;
this.result = 0
this.resultCoun t =1;
this.currentPag e =1;
}
MyPage= Page()
</script>
then in my javascript function i use object like this:

function getPage() {
if(!MyPage) {
MyPage = new Page();
}
return MyPage;
}
but there is one problem: MyPage lost one of the property, currentPage.
When i do alert(MyPage.cu urentPage) shows mi undefined. After object
initialization everything seems to be alright, currentPage is set to 1
but when i Try use MyPage in my js code is already set to undefined.
What happen? What I'm doing wrong?
thank for any help
Gregor
May 8 '06 #1
3 1840
VK

Grzegorz Slusarek wrote:
Hi all. I have situation that when my page is loaded i create js object
<html>
..
<script>
function Page() {
this.page = 0;
this.result = 0
this.resultCoun t =1;
this.currentPag e =1;
}
MyPage= Page()
</script>


You are trying to create a /new/ instance of Page object. So you have
to use the keyword "new":

function Page() {
this.page = 0;
this.result = 0
this.resultCoun t =1;
this.currentPag e =1;
}

var MyPage = new Page();

May 8 '06 #2
Sorry VK. Ofcourse i do this way just forgot to write it. Still don't
know way my object loose one property. The way that my page is
intialized is:
first all javascript is loaded
second js code run in html -> MyPage= new Page()
third i use function in js files as event handlers on my page and there
my object loose property.
Gregor
VK napisał(a):
Grzegorz Slusarek wrote:
Hi all. I have situation that when my page is loaded i create js object
<html>
..
<script>
function Page() {
this.page = 0;
this.result = 0
this.resultCo unt =1;
this.currentP age =1;
}
MyPage= Page()
</script>

You are trying to create a /new/ instance of Page object. So you have
to use the keyword "new":

function Page() {
this.page = 0;
this.result = 0
this.resultCoun t =1;
this.currentPag e =1;
}

var MyPage = new Page();

May 8 '06 #3
Grzegorz ¦lusarek wrote:
Hi all. I have situation that when my page is loaded i create js object
<html>
..
<script>
The type attribute is required:

<script type="text/javascript">

function Page() {
this.page = 0;
this.result = 0
this.resultCoun t =1;
this.currentPag e =1;
}
MyPage= Page()
Presumably you mean:

var MyPage= new Page();
Without the 'new' operator, the value of MyPage is set to the value
returned by Page(). Since Page doesn't return anything, MyPage is
undefined.
</script>
then in my javascript function i use object like this:

function getPage() {
if(!MyPage) {
MyPage = new Page();
}
return MyPage;
}
but there is one problem: MyPage lost one of the property, currentPage.
When i do alert(MyPage.cu urentPage) shows mi undefined. After object
How do you call getPage and what do you do with the returned value?
Since MyPage is undefined before getPage() is called, the if loop should
be executed and:

var newPage = getPage();
alert(newPage.c urrentPage);

should show 1 (it does for me).

initialization everything seems to be alright, currentPage is set to 1
but when i Try use MyPage in my js code is already set to undefined.
What happen? What I'm doing wrong?


The code you've posted initialises MyPage to undefined, you don't show
how you call getPage. Show the code... here's my test:

<script type="text/javascript">
function Page()
{
this.page = 0;
this.result = 0
this.resultCoun t =1;
this.currentPag e =1;
}

var MyPage;

function getPage()
{
if(!MyPage) {
MyPage = new Page();
}
return MyPage;
}

var newPage = getPage();
alert(newPage.c urrentPage);

</script>

--
Rob
May 8 '06 #4

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

Similar topics

5
2695
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
19
6937
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the JavaScript in this code from a couple different sites but I'm not 100% sure what each line is doing...This is the ASP code that I'm using for the page....Take a look at the JavaScript code and please let me know what each line is doing....I have been...
12
1936
by: Mark Fox | last post by:
Hello, I am attempting to do something very simple. I have a page MainPage.aspx and a popup window Popup.aspx. When users click on the linkbutton in the popup window I am looking to do some server side processing in Popup.aspx, then have the popup window close, and have the MainPage.aspx do a postback to the server. But I am having trouble determining what client side javascript Popup.aspx should render after it does its server side
3
1891
by: lcjohnso | last post by:
Hi all, Does anyone know if there is an easy way to create the html representation of an HTMLElement object in javascript? I'm attempting to update the innerHTML property of a div element to allow user entries to presist when the the input display pane is changed. Basically I have several different div panes that users can rotate through when entering values of a simulated form whose values are then parsed and used to dynamically...
18
1934
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
13
2974
by: Walton | last post by:
I'm wondering if anyone knows or knows where to find a collection of javascript "brain teasers" for lack of a better description. Companies use these types of tests for programmers all the time when hiring. I've managed to find a few questions here and there.. for example (i'll leave the following unanswered for your amusement): *****NOTE -- i'm not interested in answers to these, just curious as to where I can find some more. -- ...
0
9721
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
10639
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
10383
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,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
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
5550
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
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
2
3861
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.