0

first of all I am new at learning coding. In my code I wanted to create a function "Square" which creates a square object by using the length parameter that you passed in.And another function to calculate its perimeter.Here is my code and I could not find my fault can you help me ?

function Square(length){
    this.length = length;
}

var calcPerimeter = function() {
  return this.sideLength * 4;
};
var calcArea = function() {
    return this.sideLength * this.sideLength ;
};

var p = square.calcPerimeter();
var a = square.calcArea();

var calcPerimeter2 = function(n){
    return n*4;
 };
var square1 = new Square(5);
square1.calcPerimeter2();
Recomer
  • 178
  • 2
  • 12
  • 3
    I'd take a step back and maybe spin through some JS tutorials; functions you define in the global scope don't magically become functions on objects. What's `square` that you're trying to call them on? – Dave Newton Nov 27 '14 at 18:04
  • I wanted to create a Constructor which name is Square. And when i write "var square1 = new Square(2);" it would create a new square which has 2 as length. – Recomer Nov 27 '14 at 18:07
  • The following answer may be helpful, it explains prototype, constructors, inheritance, mix ins and more: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 28 '14 at 00:40
  • I got it I'll study on the link also thanks a lot for help! – Recomer Nov 28 '14 at 18:51

3 Answers3

1

Maybe this is what your looking for:

function Square(length){
    this.sideLength = length;
}

Square.prototype.calcPerimeter = function() {
  return this.sideLength * 4;
};
Square.prototype.calcArea = function() {
    return this.sideLength * this.sideLength ;
};

var square = new Square(5);

console.log(square.calcPerimeter());
console.log(square.calcArea());
CD..
  • 72,281
  • 25
  • 154
  • 163
  • Thank you very much! It was exactly what I was trying to do.So what does prototype do ?Why do you use it ? – Recomer Nov 27 '14 at 18:33
  • have a look: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype – CD.. Nov 27 '14 at 18:34
1

Try this...

    function Square(length){
     this.length = length; //Constructor
    }

    Square.prototype.calcPerimeter = function(){
      return this.length * 4;
    }

    Square.prototype.calcArea = function() {
      return this.length * this.length;
    }

   var s = new Square(5);
   s.calcPerimeter();

Using this pattern you can simulate inheritance in javascript, this pattern is called "Prototype". Hope this work for you.... Regards

  • Thanks for help! I think i will study on this "prototype" thing to make sth more clear for myself. – Recomer Nov 28 '14 at 18:53
0

I don't know what you do in this code, but I will do the code like this:

function Square(length){
    this.length = length;

    this.calcSideLength = function() {
        this.sideLength = /* Your code to calculate sideLength */;
    };

    this.calcPerimeter = function() {
        return this.sideLength;
    };

    this.calcArea = function() {
        return this.sideLength * this.sideLength;
    };

    this.calcPerimeter2 = function(n) {
        return n*4;
    };
}

var square = new Square(5);

var p = square.calcPerimeter();
var a = square.calcArea();

var square1 = new Square(5);
square1.calcPerimeter2(/* send a number */);
Dazuku
  • 273
  • 1
  • 7