View Full Version : What's wrong with this code?

11-10-2011, 03:38 PM
Hello, if you could please tell me what I did wrong to make this code not work. Can you put a variable in a function callback ?

<script type="text/javascript">
var aaa = prompt('number?')
var obja = new Const(aaa);
functin Const(numb){

11-10-2011, 03:43 PM
It's function, not functin.

Also, alert(obja.x) from within the constructor doesn't make any sense. The constructor is not supposed to know about some arbitrary instance. You probably mean alert(this.x).

11-10-2011, 04:04 PM
Ok, thanks how about this one?

var aaa = prompt('number?');
var bbb = prompt('number?');
var obja = new Const(aaa);
var objb = new Const(bbb);
function Const(aaa,bbb){

the point is to add two numbers. The numbers are obtained through a prompt, then defined to a variable, then put inside a callback function. The problem I've having is getting both in there at the same time.

11-10-2011, 04:30 PM
I remember mentioning this already: Please spend some time on working through a beginner's tutorial. If you did, all of those issues you are currently having would disappear.

You obviously set up your constructor to expect two parameters, yet instead of providing those two parameters when instantiating the object, you only provide one. Two times. That doesn't make any sense.

The rather obvious solution is

var obja = new Const(aaa, bbb);