2

Sorry, if it is duplicate, but I didn't find explanation. How can I create field in js class? to define in the future...

class Polygon {
   //var whyNot; This makes false
   constructor(height, width) {
      this.height = height;
      this.width = width;
   }

   calcArea() {
     return this.height * this.width;
   }
}
War10ck
  • 12,387
  • 7
  • 41
  • 54
nolbadi111
  • 61
  • 1
  • 1
  • 4
  • Are you using ES6, TypeScript, etc.? – War10ck May 12 '16 at 16:55
  • I don't think so - I found this example here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes and I'm trying to learn on clear document. – nolbadi111 May 12 '16 at 17:05
  • That example is ES6. I've added the tag to your question to give it more visibility. – War10ck May 12 '16 at 17:31
  • @nolbadi111 in fact it seems you are referring to a recent version of Javascript (ES2015) which is partially implemented in recent browsers. – C.Champagne May 12 '16 at 17:34
  • Sorry, your question is still a little unclear. Do you want an empty variable that you could use later or are you trying to add a private variable to a ES6 class? – War10ck May 12 '16 at 17:39

3 Answers3

0

Your understanding of JS objects is a little off... you create a variable that calls the function which is its own constructor. Then you modify the object to contain functions you wish to invoke later. like this https://jsfiddle.net/programndial/vonc5af5/

<input type="button" id="testButton" value="Test" />

function Polygon(height, width) {
    this.height = height;
    this.width = width;

    Polygon.prototype.calcArea = function(string) {
        return this.height * this.width;
    }
}

testButton.onclick = function() {
   var newPoly = new Polygon(10, 25);
   alert(newPoly.calcArea());
}
programndial
  • 157
  • 9
  • I do not create a variable that calls the function which is its own constructor. after "whyNot" varaiable is no = constructor etc. I am trying to make some field like "whyNot = 3;" but I can't. – nolbadi111 May 12 '16 at 17:12
  • @nolbadi111 im just saying your js class construction is off.. look at [this fiddle](https://jsfiddle.net/programndial/82zhgf8h/) you can do exactly what you want. – programndial May 12 '16 at 17:16
  • @nolbadi111 just remember that members declared with var are private and can only be modified by methods the 'class'.. if you want a public property you have to use this.whyNot – programndial May 12 '16 at 17:20
  • @programndial why don't you use the code given in the question? – C.Champagne May 12 '16 at 18:58
0

The answer is (partially) in your question.

We will assume from the code and the link in your comment that you use a recent version of Javascript : ES6 or ES2015.

You can create them in the constructor method as in the code you provided.

class Polygon {

   constructor(height, width) {
      //height and width will be fields of Polygon
      this.height = height;
      this.width = width;
      //...and whyNot too!
      this.whyNot = 42;
   }

   ...

Of course, since ES6 is compatible the previous versions of Javascript you still can use constructor methods, object literals...

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
-2

Check this answer. Constructors in JavaScript objects

You don't have a 'class' in javascript, you define the object as a function. you define inheritable functions as prototypes. pretty much you would use 'prototype' keyword for oop behavior.

Community
  • 1
  • 1
Arulvel
  • 69
  • 3
  • 2
    As of ES6, JavaScript does have classes: http://stackoverflow.com/questions/28308578/member-variables-in-es6-classes – War10ck May 12 '16 at 16:58