473,383 Members | 1,868 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,383 software developers and data experts.

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.resultCount =1;
this.currentPage =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.cuurentPage) 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 1822
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.resultCount =1;
this.currentPage =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.resultCount =1;
this.currentPage =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.resultCount =1;
this.currentPage =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.resultCount =1;
this.currentPage =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.resultCount =1;
this.currentPage =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.cuurentPage) 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.currentPage);

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.resultCount =1;
this.currentPage =1;
}

var MyPage;

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

var newPage = getPage();
alert(newPage.currentPage);

</script>

--
Rob
May 8 '06 #4

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

Similar topics

5
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...
19
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...
12
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...
3
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...
18
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...
13
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.