By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,523 Members | 1,938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,523 IT Pros & Developers. It's quick & easy.

Javascript object and lost property

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.