javascript

메서드 체인

정원쓰 2020. 3. 13. 17:32

메서드가 객체를 반환하게 되면, 메서드의 반환 값인 객체를 통해 또 다른 함수를 호출

 

 <script>   
        var MySquare = function(){
            this._width = 0;
            this._height = 0;
            this._x = 0;
            this._y = 0;
            this._color = "";
        }

        MySquare.prototype.setWidth = function( _w ){
            this._width = _w;
        };

        MySquare.prototype.setHeight = function( _h ){
            this._height = _h;
        };

        MySquare.prototype.setX = function( _x ){
            this._x = _x;
        } 

        MySquare.prototype.setY = function( _y ){
            this._y = _y;
        }

        MySquare.prototype.setColor = function( _c ){
            this._color = _c;
        }

        MySquare.prototype.getInfo = function(){
            console.log( this._width );
            console.log( this._height );
            console.log( this._x );
            console.log( this._y );
            console.log( this._color );
        }
        
        //객체 값 지정 및 호출
        var _square = new MySquare();
        _square.setWidth( 100 );
        _square.setHeight( 100 );
        _square.setX( 100 );
        _square.setY( 100 );
        _square.setColor( "red" );
        _square.getInfo();


    </script>