1

I'm not sure what this means or how to word the question correctly.

In the first example I could put the variable before the function "numbers1.forEach(...)" but in the second example I could not "num.square()"?

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

var numbers2 = [1, 2, 3, 4, 5], sum = 0;
forEach(numbers2, function(number) {
  sum += number;
});
console.log(sum);
// → 15

var numbers1 = [1, 2, 3, 4, 5], sum = 0;
numbers1.forEach(function(number) {
  sum += number;
});
console.log(sum);
// → 15
// this works!

But this does not work

var square = function(x) {
  return x * x;
};

var num = 12;
console.log(square(num));
// → 144

console.log(num.square());
// → TypeError: undefined is not a function (line 9)
// does not work?

Thanks!

2
  • 3
    forEach is a built-in method on Arrays. numbers1.forEach() is calling a different method than forEach(numbers2). Commented Mar 4, 2015 at 14:42
  • 1
    forEach is a method of array variables, while square is your own function and not a method of the integer variables. Commented Mar 4, 2015 at 14:42

3 Answers 3

2

Syntax-wise, there's no "magic" happening here. You're describing two very different things.

In your first example, you're passing an Array to your own defined forEach method. In the second example, the forEach you're calling is actually a built-in method on Arrays (see MDN). If you changed your forEach to not invoke action, you'll see that yours breaks and the second one still works.

You can achieve this functionality yourself. You could add a function to Number to make your second example work (although this is not a recommended practice, as it could collide with future JavaScript features [and many other reasons] or break existing functionality):

Number.prototype.square = function() {
      return this * this;
};

var num = 12;
var squared = num.square();

alert(squared);

Sign up to request clarification or add additional context in comments.

2 Comments

@unobf: Agreed, I added a brief notice in my answer.
Ah thank you! I forgot about "built-in methods." Cleared up all my confusion.
0

If you remove the forEach function from your first example, it will still work, that is because the Array type has a builtin forEach function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Alternatively try to change your forEach function to each and you will see that it will no longer work.

What you are trying to do is add a new method to an existing type. This is not good practice in JavaScript.

Comments

0

The example with the array works because Array in JavaScript (ECMAScript5) defines a forEach function.

The example with the number doesn't work because Number doesn't define a square function. You can extend Number to include the square function like this:

Number.prototype.square = function(x) {
  return this * this;
};

2 Comments

Why submit in the first place if you know you are gonna edit it? Why not just wait with the initial submit?
I think you mean Number.prototype.square?

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.