Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question A question about prototype object usage for OOP?

    Hello,

    I'm trying to write my class with member properties and functions like below. What I want to learn is that is there any problem to use prototype object in the class that we want to use with it? I asked because I generally see that prototype object definition and member functions and properties created outside of the class unlike my class that I wrote like below.

    Is there any problem writing my class like below?
    Thanks.


    Code:
            function Point()
    	{
    		Point.prototype.x = this._x;
    		Point.prototype.y = this._y;
    		
    		function _setX(x)
    		{
    			this.x = x;
    		}
    		Point.prototype.setX = _setX;
    		
    		
    		function _setY(y)
    		{
    			this.y = y;
    		}
    		Point.prototype.setY = _setY;
    		
    		
    		function _show()
    		{
    			alert(this.x + this.y);
    		}
    		Point.prototype.Show = _show;
    	}
    
    
    	function callMyClass()
    	{
    		var p = new Point();
    		p.setX(3);
    		p.setY(5);
    		p.Show();
    	}

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    yes. the prototype is the code every object inherits. there is no need to repeat that in every object (instance) creation. therefore you write it outside the constructor. it’s also superfluous to write a function and later assign that to the prototype.

    PHP Code:
    function Point() {}
    // this.x & this.y can be assigned at runtime

    Point.prototype.show = function() {
        
    alert(this.this.y);
    }

    // unless you want some value verification for x & y, you don’t need a setter

    var = new Point;
    p.3;
    p.5;
    p.show(); 
    PHP Code:
    // using a setter with value verification (as of ECMAScript 262 5th Edition)
    function Point()
    {
        function 
    toNumber(num)
        {
            
    num parseFloat(num);
            if (
    isFinite(num) && !isNaN(num)) {
                return 
    num;
            }
            throw new 
    TypeError("coordinate is not a number.");
        }
        var 
    xy;
        
    Object.defineProperty(this"x", {
            
    get: function() { return x; }, 
            
    set: function(val) { toNumber(val); }
        });
        
    Object.defineProperty(this"y", {
            
    get: function() { return y; }, 
            
    set: function(val) { toNumber(val); }
        });
    }

    Point.prototype.show = function() {
        
    alert(this.this.y);
    }

    var 
    = new Point;
    // throws an error, if coordinates do not evaluate as number
    p.3;
    p.5;
    p.show(); 
    Last edited by Dormilich; 12-28-2011 at 11:09 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much Dormilich, it helped me a lot and works like a charm. But one of my aim was to be able to benefit from intellisense to access my properties or local variables. Obviously you taught me to best practise to define properties but when I try to access to x and y variables by p object, Visual Studio's and Dreamweaver's intellisense haven't shown them.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    part of the problem is that JavaScript is a prototype-based Programming Language. Intellisense based on class-type languages (C, Java, etc.) can’t be applied without losing much of JS’s possibilities (e.g. changing/extending objects on run-time). or in other words: there are no classes in JS.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •