Adsense

Javascript constructor example

 function Point(x, y) {
        this.x = x;
        this.y = y;
        this.dist = function () {
            return Math.sqrt((this.x*this.x)+(this.y*this.y));
        };
        this.toString = function () {
            return "("+this.x+", "+this.y+")";
        };
    }


function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    Point.prototype = {
        dist: function () {
            return Math.sqrt((this.x*this.x)+(this.y*this.y));
        },
        toString: function () {
            return "("+this.x+", "+this.y+")";
        }
    }



function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    Point.prototype.dist = function () {
        return Math.sqrt((this.x*this.x)+(this.y*this.y));
    };
    Point.prototype.toString = function () {
        return "("+this.x+", "+this.y+")";
    };


> var p = new Point(2, 2)
    > p.constructor
    [Function: Point]
    > p.constructor.name
    'Point'

1 comment:

  1. Nice blog.
    The details that you mentioned in the blog regarding Reactjs is very easy to understand and is also very useful. Anyone who is looking to learn about react JavaScript can read and understand about this tech language for its use in development. I was also looking for dedicated reactjs developer and found your blog.
    Thanks for sharing such a great blog.
    ReactJS Development Company India
    Web Development Company in India

    ReplyDelete

comment here

newest questions on wordpress