0

the following is the code I have written

var nums = [1, 5, 4, 2, 3];
var sortedNums = [1, 2, 3, 4, 5];

var sorter = function MakeSorter() {
    'sortNums': function(nums) {
        return this.nums.sort();
    };
};

QUnit.test("Numeric list can be sorted", function(assert) {
    var sorted = sorter.sortNums(nums);
    assert.deepEqual(sorted, sortedNums, "Passed!");
});

My supposition are as follows:

  1. sorter is a function object referencing MakeSorter() function

  2. sortNums is a property of MakeSorter which turns out to be an object and hence has the function syntax

but it produces the error as expected ";" on the line where sortNums is declared.why?

5
  • 4
    The error is a syntax error, you can't just throw something that looks like an object literal property inside a function. Commented Apr 25, 2015 at 18:34
  • I can understand that @adeneo, what is the correct syntax then? Commented Apr 25, 2015 at 18:35
  • Either an object literal, not a function, or this.sortNums = function(){.. Commented Apr 25, 2015 at 18:36
  • But why do you need an inner function at all, it's not like you're really using the outer function for anything ? Commented Apr 25, 2015 at 18:37
  • @DineshSashikanth pastebin.com/sH4fti3N Commented Apr 25, 2015 at 18:37

2 Answers 2

1

Perhaps you meant to use this:

var nums = [1, 5, 4, 2, 3];
var sortedNums = [1, 2, 3, 4, 5];

var MakeSorter = function () {
    this.sortNums = function(nums) {
        return this.nums.sort();
    }
};

var sorter = new MakeSorter();

QUnit.test("Numeric list can be sorted", function(assert) {
    var sorted = sorter.sortNums(nums);
    assert.deepEqual(sorted, sortedNums, "Passed!");
});

You seem to of got the idea that in JavaScript, functions are object, but slightly different. What you are trying to make is kind of like a JavaScript class. You are making function as regular, not an object. In order to add a function to a function use this. That's short for:

var MakeSorter = function () {
   //...
}

MakeSorter.sortNums = function (nums) {
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

0

This would set the property sortNums on sorter to the function that you provided. However, I'm not exactly sure what you are trying to do in accomplishing this and there may be a better way to design the behavior you're trying to accomplish.

  var nums = [1, 5, 4, 2, 3];
  var sortedNums = [1, 2, 3, 4, 5];

  var sorter = function MakeSorter() {};
  sorter.sortNums = function(nums) {
      return this.nums.sort();
  };

  QUnit.test("Numeric list can be sorted", function(assert) {
      var sorted = sorter.sortNums(nums);
      assert.deepEqual(sorted, sortedNums, "Passed!");
  });

In short, your syntax error seems to be that you are mixing up a function body declaration with an object literal. The statements inside a function body (between the { and }) are executed when the function is called. You cannot define properties on the function by adding them in object literal syntax { prop : value }.

var sorter = {
  sortNums: function(nums) {
      return this.nums.sort();
  }
};

might be more along the lines of what you are looking for if sorter does not need to be callable.

4 Comments

var sorter = function MakeSorter() {}; is weird syntax event though valid
It is valid syntax if recursion is desired but yes, extraneous in this case.
A javascript class seems unnecessary here. You could just define a bare sortNums function, or define it as an object var sorter = {} and the rest would make sense. If you're going to make it a class, you should follow the pattern properly. Eg, function MakeSorter() {};, then define the member function on the prototype MakeSorter.prototype.sortNums = function... and instantiate using the new operator var sorter = new MakeSorter();
Since sortNums doesn't rely on the state of the class, it could be "statically" assigned as a property of the class constructor. The constructed instances will not have the method inherited but static functions should be invoked statically anyhow. The design of this likely relies on your coding convention

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.