Primitive types
represent simple values stored directly in the variable object for a given
context.
These are the closest thing to classes in JavaScript, and
objects are instances of reference types.
JavaScript use lexical-scope: the place where the variable was defined at author-time determines it's scope. In JavaScript, scopes are delimited by function blocks. ECMAScript 6 introduces the let keyword to define variables at block-scope.
var test = 'I am in the global scope';
function inner() {
isGlobal = 'Sad, but true';
var a = 1;
var test = 'I belong to the inner() function scope';
console.log(test);
}
inner(); // output: 'I belong to the inner() function scope'
console.log(test); // output: 'I am in the global scope'
console.log(isGlobal); // output: 'Sad, but true'
console.log(a); // ReferenceError: a is not defined
'use strict';
var i;
var x = 3;
var y = 2;
if (x > y) {
let z = x + y;
i = z;
}
console.log(i); // output: 5
console.log(z); // ReferenceError
Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.
var a = 2;
function foo() {
var b = 4;
function innerFoo() {
return a + b;
}
return innerFoo;
}
var f = foo();
f(); // output: 6
Some good use cases for closures are:
Determining the this binding for an executing function requires
finding the direct call-site of that function. Once examined, four rules
can be applied to the call-site, in this order of precedence
Used new operator? Uses the newly constructed object.
var obj = {
x: 81,
getX: function() { return this.x; }
};
// output: 81
console.log(obj.getX.bind(obj)());
console.log(obj.getX.call(obj));
console.log(obj.getX.apply(obj));
var name = 'Richard';
function sayHi() {
return 'Hi ' + this.name;
}
// output: Hi Richard
sayHi();
// output: Hi Peter
sayHi.call({ name: 'Peter' });
// output: Hi John
sayHi.apply({ name: 'John' });
// output: Hi Mike
sayHi.bind({ name: 'Mike' })();
JavaScript implements inheritance through prototype chaining. A prototype chain is created between object when the [[Prototype]] property of one of them is set equal to another. Generic objects inherit from the Object prototype.
function Person(name) {
this.name = name;
}
Person.prototype.sayName = function() {
return this.name;
}
function Hero(name) {
// constructor stealing
Person.call(this, name);
}
// Setting the prototype to a new instance of Person
Hero.prototype = new Person;
/*
var hero = new Hero('Superman');
console.log(hero.__proto__) // => Object
console.log(hero.constructor) // => Person
*/
// Setting the prototype to Person.prototype
Hero.prototype = Person.prototype;
/*
var hero = new Hero('Superman');
console.log(hero.__proto__) // => Person
console.log(hero.constructor) // => Person
*/
// Using Object.create method
Hero.prototype = Object.create(Person.prototype, {
constructor: {
value: Hero,
writable: true,
configurable: true,
enumerable: true
}
});
/*
var hero = new Hero('Superman');
console.log(hero.__proto__) // => Hero
console.log(hero.constructor) // => Hero
*/
We have a two way data binding when a the data can be modified both by the view and the model itself, generating a live view. The view is a projection of the model at all times. When the model changes, the view reflects the change, and vice versa.
The main purpose of one-time binding is to provide a way to create a binding that gets unregistered and frees up resources once the binding is complete. If the expression will not change once set, it is a candidate for one-time binding.
Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.
Angular services are:
Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies.
To manage the responsibility of dependency creation, each Angular application has an injector. The injector is a service locator that is responsible for construction and lookup of dependencies.
The application code simply declares the dependencies it needs on the constructor, without having to deal with the injector.
A directive is just a function which executes when the compiler encounters it in the DOM.
At a high level, directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS's to attach a specified behavior to that DOM element or even transform the DOM element and its children.
JavaScript is a dynamically typed language which comes with great power of expression, but it also comes with almost no help from the compiler.
Angular makes testing your applications easy. So there is no excuse for not testing.
Unit testing, as the name implies, is about testing individual units of code. It help us to answer questions like "Did I think about the logic correctly?" or "Does the sort function order the list in the right order?"
Angular provides several tools for writing our unit tests, among these we can find: Karma, Jasmin and angular-mocks
Karma is a Node.js command line tool that can be used to spawn a web server which loads your application's source code and executes your tests. You can configure Karma to run against a number of browsers, which is useful for being confident that your application works on all browsers you need to support. Karma is executed on the command line and will display the results of your tests on the command line once they have run in the browser.
Jasmine is a behavior driven development framework for JavaScript that has become the most popular choice for testing Angular applications. It provides functions to help with structuring your tests and also making assertions. As your tests grow, keeping them well structured and documented is vital, and Jasmine helps achieve this.
Angular provides the ngMock module, which provides mocking for your tests. This is used to inject and mock Angular services within unit tests. In addition, it is able to extend other modules so they are synchronous. Having tests synchronous keeps them much cleaner and easier to work with. One of the most useful parts of ngMock is $httpBackend, which lets us mock XHR requests in tests, and return sample data instead.
PhantomJS is a headless webkit browser, scriptable with a
JavaScript API. It has native support for various web standards such as:
Teaching, mentoring, and knowledge sharing: I'm able to transmit knowledge, I have a background on IT education at Cisco Certified Network Associate course.