creating objects in javascript

Y

yawnmoth

Say I have the following code snippet:

<script>
function a() {
this.b = 'test';

function c() {
return this.b;
}

//this.c = function() {
// return this.b;
//}
}

var d = new a();
alert(d.c());
</script>

Based on <http://www.sitepoint.com/article/oriented-programming-1/3>,
it seems like the uncommented code should work, yet as is, the only
code that does is the uncommented out code. Any idea as to why?
 
D

David Mark

Say I have the following code snippet:

<script>
function a() {
this.b = 'test';

function c() {
return this.b;
}

This is just wrong. It does not create a method of a.
//this.c = function() {
// return this.b;
//}

This is right.
}

var d = new a();
alert(d.c());
</script>

Based on <http://www.sitepoint.com/article/oriented-programming-1/3>,
it seems like the uncommented code should work, yet as is, the only

I haven't read that article, but if it indicates that your uncommented
method should work, then it is wrong.
code that does is the uncommented out code. Any idea as to why?

I assume you mean the "commented out" code.
 
H

Hendri Kurniawan

Say I have the following code snippet:

<script>
function a() {
this.b = 'test';

function c() {
return this.b;
}

//this.c = function() {
// return this.b;
//}

}

var d = new a();
alert(d.c());
</script>

Based on <http://www.sitepoint.com/article/oriented-programming-1/3>,
it seems like the uncommented code should work, yet as is, the only
code that does is the uncommented out code. Any idea as to why?


You forgot to add this.c = c;
to become

function a() {
this.b = 'test';

function c() {
return this.b;
}

this.c = c;
}

Hendri Kurniawan
 
Y

yawnmoth


Another quick question:

http://developer.mozilla.org/en/docs/Setting_HTTP_request_headers#Summary

In that URL, they do something like this:

<script>
var myObject = {
myFunction: function() {
return 'zzz';
},

get internalFunction() {
return 'yyy';
},

mySecondFunction: function() {
return this.internalFunctions();
}
};

alert(myObject.myFunction());
alert(myObject.mySecondFunction());
</script>

mySecondFunction(), however, doesn't do anything. What is "get
internalFunction" (as is sorta done in the above URL) supposed to do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top