Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do projects like angular have their own version of common functions?

I am trying to understand or refresh my logic on this better - for example in angular it has the angular.forEach().

I thought it was because the code in a controller(or module in general) - didn't have access to the browser api (functions and objects, etc) - and for that matter the forEach function of the browser.

But just tested it out as I was trying to understand it better/prove rationale - and both of these console.log() expressions worked.

angular.module('myApp', [])
  .controller('JCtrl', ['$scope', function($scope) {
    $scope.test = 'scope and binding works'; 

    [0, 1, 4].forEach(function(value) {
      console.log(value);
    });
    console.log([].forEach);
}]);

here is the plnkr

like image 280
jamie Avatar asked Feb 21 '15 18:02

jamie


People also ask

Why we use angular instead of JavaScript?

AngularJS empowers developers to build rich internet applications (RIAs) more easily. Where many JavaScript frameworks focus on expanding the capabilities of JavaScript itself, AngularJS instead provides methods for enhancing HTML. AngularJS expands the capabilities of HTML beyond being a simple markup language.

Is angular a language or framework?

Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript. It implements core and optional functionality as a set of TypeScript libraries that you import into your applications.

What is the difference between angular and JavaScript?

JavaScript is a scripting language that is used to generate dynamic HTML pages with interactive effects on a webpage that runs in the web browser of the client. On the other hand, Angular JS is a framework that is built on JavaScript and adds new functionalities to HTML.

What is angular good for?

Angular helps build interactive and dynamic single page applications (SPAs) through its compelling features that include templating, two-way binding, modularization, RESTful API handling, dependency injection, and AJAX handling.


1 Answers

Directly from github source code of Angular.js that you can find here

/**
* @ngdoc function
* @name angular.forEach
* @module ng
* @kind function
*
* @description
* Invokes the `iterator` function once for each item in `obj` collection, which can be either an
* object or an array. The `iterator` function is invoked with `iterator(value, key, obj)`, where `value`
* is the value of an object property or an array element, `key` is the object property key or
* array element index and obj is the `obj` itself. Specifying a `context` for the function is optional.
*
* It is worth noting that `.forEach` does not iterate over inherited properties because it filters
* using the `hasOwnProperty` method.
*
* Unlike ES262's
* [Array.prototype.forEach](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18),
* Providing 'undefined' or 'null' values for `obj` will not throw a TypeError, but rather just
* return the value provided.
*
  ```js
    var values = {name: 'misko', gender: 'male'};
    var log = [];
    angular.forEach(values, function(value, key) {
      this.push(key + ': ' + value);
    }, log);
    expect(log).toEqual(['name: misko', 'gender: male']);
  ```
*
* @param {Object|Array} obj Object to iterate over.
* @param {Function} iterator Iterator function.
* @param {Object=} context Object to become context (`this`) for the iterator       function.
* @returns {Object|Array} Reference to `obj`.
*/

So, as you can see

Unlike ES262's Array.prototype.forEach, providing 'undefined' or 'null' values for obj will not throw a TypeError, but rather just return the value provided.

These kind of things are done because when you design your own system, there is a high possibility that you will need something more other than the original.

like image 119
Süha Boncukçu Avatar answered Oct 04 '22 14:10

Süha Boncukçu