Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Помогите новичку в объектном JS (https://javascript.ru/forum/misc/23366-pomogite-novichku-v-obektnom-js.html)

popov654 21.11.2011 16:42

Помогите новичку в объектном JS
 
Всем добрый день,
У меня новый студпроект, нужно для браузера создать некое подобие UML-редактора. Начать решил с рисования простых фигур, нашёл для этого библиотечку. Порисовать с её помощью получилось, но как только начал писать код, отвечающий за логику, сразу всё перестало работать. При этом IE никаких ошибок не выдавал, судя по отладочной печати, просто на определённом месте сценарий переставал выполняться. Сейчас глянул Консоль ошибок в Opera - она выдаёт такую ошибку:

Inline script thread
Uncaught exception: TypeError: Cannot convert 'this.arguments' to object
Error thrown at line 6, column 16 in Shape(type, color, x, y, width, height) in http://popov654.pp.ru/qreal/QRealWeb/index.php:
if (this.arguments.length == 2)
called from line 40, column 13 in http://popov654.pp.ru/qreal/QRealWeb/index.php:
Circle.prototype = new Shape('circle')

Вот код:

<div id="Canvas" style="border: 1px solid black;position:absolute;left:43px;top:40px;height:420px;width:920px;"></div>
        <script type="text/javascript">
            var jg = new jsGraphics("Canvas");    // Use the "Canvas" div for drawing
             
            function Shape(type, color, x, y, width, height) {
                this.type = type
                if (this.arguments.length == 2) {
                   this.color = color
                } else {
                   this.color = '#DADADA'
                }
                if (this.arguments.length == 4) {
                    this.x = x
                    this.y = y
                } else {
                    this.x = document.getElementById('Canvas').style.width / 2
                    this.y = document.getElementById('Canvas').style.height / 2
                }
                if (this.arguments.length == 6) {
                    this.width = width
                    this.height = height
                } else {
                    this.width = 22
                    this.height = 22
                }
             }

             function Circle(color, x, y, width, height) {
                 this.color = color
                 this.x = x
                 this.y = y
                 this.width = width || 22
                 this.height = height || 22
                 this.draw = function() {
                     jg.setColor(color)
                     jg.fillEllipse(x, y, width, height);
                     jg.paint()
                 }
                 this.draw()
             }
             Circle.prototype = new Shape('circle')

             function Triangle(color, x, y, width, height) {
                 this.color = color
                 this.x = x
                 this.y = y
                 this.width = width || 22
                 this.height = height || 22
                 this.draw = function() {
                     jg.setColor(color)
                     jg.fillPolygon(new Array(x, x + width / 2, x + width), new Array(y + height, y, y + height));
                     jg.paint()
                 }
                 this.draw()
             }
             Triangle.prototype = new Shape('triangle')

             function Rectangle(color, x, y, width, height) {
                 this.color = color
                 this.x = x
                 this.y = y
                 this.width = width || 22
                 this.height = height || 22
                 this.draw = function() {
                     jg.setColor(color)
                     jg.fillRect(x, y, width, height);
                     jg.paint()
                 }
                 this.draw()
             }
             Rectangle.prototype = new Shape('rectangle')



             var protos = [new Circle('#00FF00', 78, 65, 22, 22),
                           new Triangle('#CC0000', 118, 65, 22, 22),
                           new Rectangle('#0000FF', 162, 65, 22, 22)]

             

             jg.setColor('black');
             jg.drawRect(50, 40, 200, 60);
             jg.setFont("Verdana", "14px", Font.BOLD);
             jg.drawString("Palette", 56, 43);
             jg.setColor("green");
             jg.fillEllipse(78, 65, 22, 22);
             jg.setColor("maroon");
             jg.fillPolygon(new Array(118,129,140), new Array(87,65,87));
             jg.setColor("blue");
             jg.fillRect(162, 65, 22, 22);
             jg.drawImage("logo_web.png", 300, 100, 294, 190);
             jg.paint();
    </script>


Где я ошибся?

Kolyaj 21.11.2011 16:46

А что такое this.arguments?

popov654 21.11.2011 16:47

А, всё, разобрался. this.arguments писать нельзя, т.к. this указывает на создаваемый объект, а не на саму функцию.

popov654 21.11.2011 16:49

:)
Как-то сегодня быстро решилось, вчера полночи сидел, разные строчки в коде менял в поисках ошибки... Видимо, эта была не единственной, так как вначале делал без this, и не работало, поэтому и решил его добавить ради эксперимента :D


Часовой пояс GMT +3, время: 20:40.