0

I'm trying to make a class that has an optional parameter.

Is it possible to add a conditional such as if(!(degrees === undefined)) {this._items.degree = degrees}

When I try to do this, I get the error "SyntaxError: Unexpected identifier" upon declaring/creating the object.

var element = {
    degree = 0;
}

var Radial = function(items, degrees) {
    this._items = itemsfunction(); //_items is an element structure the function makes and returns the element
    if (!(degrees===undefined)) { //where the error occurs
        for (int i = 0; i<this._items.length; i++) {
            if (!(degrees[i]===undefined)) {
                this._items[i].degree = degrees[i];
            } else {
                this._items[i].degree = 0;
            }
        }
    }
};

The above is my constructor

I declare it as so radial = new Radial(item, degrees); and radial = new Radial(item); and both return the same error of Unexpected identifier in the console, and the Radial object is not created. When I take out the if conditional, everything works.

What am I doing wrong? I'm new to javascript classes.

1
  • 1
    Error probably is the = in the object prop assignment. Should be : Commented Mar 23, 2017 at 1:05

1 Answer 1

4

Your problem is not with the conditional, it's with the line below:

for(int i = 0; i<this._items.length; i++){
//  ^^^

That needs to be var. Also, the object literal should be

… {
    degree: 0
}; //     ^^^^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.