function foo() {
if (false) {
var a = 1;
}
console.log(a);
}
foo(); // undefined
function bar() {
if (false) {
let a = 1;
}
console.log(a);
}
bar(); // Reference Error
function foo() {
var a;
if (false) {
a = 1;
}
console.log(a);
}
foo(); // undefeind
function bar() {
if (false) {
let a;
a = 1;
}
console.log(a);
}
bar(); // Reference Error
const a = 1;
a = 2;
// TypeError:
// Assignment to constant variable.
const b;
// SyntaxError:
// Missing initializer in const declaration
var foo = function(param) {
return true;
}
const foo = (param) => {
return true;
}
const foo = param => true;
let arr = [1,2,3,4,5];
arr.map(m = n * n)
.filter(n => n % 2 === 0)
.reduce((r, n) => r + n); // [4, 16]
const protocol = 'http';
const ip = '127.0.0.1';
const port= 3000;
const resource = 'user';
const id = '402';
let url;
url = protocol + '://' + ip + ':' + port + '/' + resource + '/' + id;
url = `${protocol}://${ip}:${port}/${resource}/${id}`
const async1 = r => {
return new Promise(rev => {
setTimeout(() => {
rev(r)
}, 1000);
});
};
const async2 = r => Promise.resolve(r + 1);
const async3 = r => Promise.resolve(r + 2);
Promise.resolve(1)
.then(async1)
.then(async2)
.then(async3)
.then(console.log); // 4
Promise.all([
async1(1),
async2(1),
async3(1),
]).then(console.log); // [1, 2, 3]
filter('capitalize', () => {
return v => {
if (!v) return '';
return v.charAt(0).toUpperCase() + v.slice(1);
};
});
$filter
git checkout filters
directive('todoTextValidator', () => {
return {
restrict: 'A',
require: 'ngModel',
link: (scope, elem, attrs, modelCtrl) => {
modelCtrl.$validators.todoText = (modelValue, viewValue) => {
if (viewValue) return /^(:\w+\s)/.test(viewValue);
}
}
};
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul>
<li><a href="africa">Africa</a></li>
<li><a href="asia">Asia</a></li>
<li><a href="europe">Europe</a></li>
<li><a href="north-america">North America</a></li>
<li><a href="oceania">Oceania</a></li>
<li><a href="south-america">South America</a></li>
</ul>
<div id="content"></div>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
const load = country => {
document.getElementById('content').innerHTML = `${country} is loaded`
document.title = country;
};
window.addEventListener('click', e => {
e.preventDefault();
const country = e.target.text;
history.pushState({country: country}, `title: ${country}`, country);
load(country);
});
window.addEventListener('popstate', evt => {
var state = evt.state;
if (state !== null) load(state.country)
});
factory('myService', function() {
var intstant = {
users: ['Alice', 'Bek', 'Chris']
};
return intstant;
});
factory('myService', function() {
this.users = ['Alice', 'Bek', 'Chris'];
});
$provide.factory('myHttpInterceptor', function() {
return {
'request': function(config) {return config;},
'requestError': function(rejection) {
return $q.reject(rejection);
},
'response': function(response) {return response;},
'responseError': function(rejection) {
return $q.reject(rejection);
}
};});
$httpProvider.interceptors.push('myHttpInterceptor');
angular.module('todomvc')
.factory('todoStorage', ($http) => {
const storage = {
data: [],
get() {
$http.get('http://localhost:3002/v1/todos')
.then(result => angular.copy(result.data, this.data))
.catch(err => console.error(err));
return this.data;
},
create(title) {
$http.post('http://localhost:3002/v1/todos', {title: title, done: false})
.then(result => this.data.push(result.data))
.catch(err => console.error(err));
},
destory(todo) {
$http.delete(`http://localhost:3002/v1/todos/${todo.id}`)
.then(result => angular.copy(this.data.filter(t => t.id !== todo.id), this.data))
.catch(err => console.error(err));
},
update(todo) {
const reqBody = {title: todo.title, done: todo.done};
$http.put(`http://localhost:3002/v1/todos/${todo.id}`, reqBody)
.then(result => undefined)
.catch(err => console.error(err));
}
};
return storage;
});