Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22632

Re: Objects in javascript

Newsgroups comp.lang.javascript
Subject Re: Objects in javascript
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References <fe9153d3-cec8-4916-a310-e98b2a145373@googlegroups.com> <XnsA2B477051A388eejj99@194.109.133.133> <7973890c-de85-4812-9b20-471e82b53925@googlegroups.com>
Date 2014-01-15 23:28 +0100
Message-ID <XnsA2B6EED7F7FD9eejj99@194.109.133.133> (permalink)

Show all headers | View raw


jonas.thornvall@gmail.com wrote on 15 jan 2014 in comp.lang.javascript:

> Den måndagen den 13:e januari 2014 kl. 11:42:00 UTC+1 skrev Evertjan.:
>> jonas.thornvall@gmail.com wrote on 13 jan 2014 in comp.lang.javascript:
>> > Could someone pont me to a link, where one can see howto create, read
>> > and write to objects in java. 
>> 
>> Wrong NG.
>> We know nothing about Java here.
> 
> I have some code that i want to change into object oriented code.

You are not responding to the quote above, why then do you quote?
 
> But i am stuck already at the start, i was Think this would reach the
> first object in the Array and write out a green rectangle? 
> 
> Usually when i have a working example, i have no trouble start
> converting. So could anyone fix what is wrong in the following, example
> snippet. 

The console of Chrome shows two consecutive errors:

1 the arr should be var-ed global, not inside the function.

> 
> <!DOCTYPE HTML>
> <html>
>   <head>
>     <style>
>       body {
>         margin: 0px;
>         padding: 0px;
>       }
>     </style>
>   </head>
>   <body>
>     <canvas id="myCanvas" width="578" height="200"></canvas>
> 
> <script>

var arr = [];

> function create_objects() {
> 
//  var arr = []; // not here
> var len = 5;
> for (var i = 0; i < len; i++) {
>     var obj = {
>         number: i,
>         fcolor: 'black',
>         rheight: 100,
>         rwidth: 100,
>         rcolor: 'green'
>     };
>     arr.push(obj);
>}
>}
>       create_objects();
>       var canvas = document.getElementById('myCanvas');
>       var context = canvas.getContext('2d');
>       context.beginPath();
>       context.rect(188, 50, 200, 100);
>       //context.fillStyle = 'yellow';
>       context.fillStyle = arr[1].obj.rcolor;

2 this is nonsense, "obj" does not exist outside the create_objects(),
as you push the content of "obj". The console errros should have given it 
away to you.

do:

context.fillStyle = arr[1].rcolor;



>       context.fill();
>       context.lineWidth = 7;
>       context.strokeStyle = 'black';
>       context.stroke();
>     </script>
>   </body>
> </html>

So next time use the error console. 



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Objects in javascript jonas.thornvall@gmail.com - 2014-01-12 23:07 -0800
  Re: Objects in javascript Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2014-01-13 10:11 +0100
  Re: Objects in javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-13 11:42 +0100
    Re: Objects in javascript "se" <se@no-one.here> - 2014-01-13 13:25 +0100
    Re: Objects in javascript jonas.thornvall@gmail.com - 2014-01-15 13:59 -0800
      Re: Objects in javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-15 23:28 +0100
        Re: Objects in javascript jonas.thornvall@gmail.com - 2014-01-15 15:02 -0800
  Re: Objects in javascript Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-13 13:48 +0100
    Re: Objects in javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-13 14:31 +0100

csiph-web