Вопрос:
Почему функция внутри Block не всегда перезаписывает глобальный идентификатор?
Код:
/// пример 1
{
function a() {}
}
console.log(a); /// f a() {}Вопрос:
Почему функция внутри Block не всегда перезаписывает глобальный идентификатор?
Код:
/// пример 1
{
function a() {}
}
console.log(a); /// f a() {}Очень интересная тема, на которой я давненько съел собаку при изучении. Теперь хотелось бы помочь закрыть чей-нибудь гештальт при изучении javascript.
Так как поведение внутренних механизмов изменилось со временем, то я обязан учесть этот факт. Итак начнем мы с ядра этого механизма, а затем рассмотрим частные случаи.
Алгоритм ToPrimitive(input[,preferredType]):
The abstract operation ToPrimitive takes argument input (an ECMAScript language value) and optional argument preferredType (string or number). It converts its input argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the optional hint preferredType to favour that type. It performs the following steps when called:
1. If Type(input) is Object, then
a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).Вопрос: Почему мы должны применить деструктуризацию параметра чтобы получить массив который передаем при создании.
Код:
const trapsObject = {
construct(target, [args]){
console.log(args); /// [1,2,3]
return {};
}
};
const Exo = new Proxy(Array, trapsObject);
| function shallowMapEquality(map1, map2){ | |
| if(!(map1 instanceof Map) || !(map2 instanceof Map)) | |
| throw Error("One of arguments or both is/are not instance of Map"); | |
| if(map1 === map2) return true; | |
| let it1 = map1[Symbol.iterator](); | |
| let it2 = map2[Symbol.iterator](); | |
| while(true){ | |
| let {value: v1, done: d1} = it1.next(); | |
| let {value: v2, done: d2} = it2.next(); | |
| if(d1 === true || d2 === true) { |
| function shallowSetEquality(set1, set2){ | |
| if(!(set1 instanceof Set) || !(set2 instanceof Set)) | |
| throw Error("One of arguments or both is/are not instance of Set"); | |
| if(set1 === set2) return true; | |
| let it1 = set1[Symbol.iterator](); | |
| let it2 = set2[Symbol.iterator](); | |
| while(true){ | |
| let {value: v1, done: d1} = it1.next(); | |
| let {value: v2, done: d2} = it2.next(); | |
| if(d1 === true || d2 === true) { |
| function rand(start, end){ | |
| return start + (Math.round(Math.random() * Math.abs(start - end))); | |
| } |
| function* generatorColors(options) { | |
| options = Object.assign( | |
| { | |
| order: ["r", "g", "b"], | |
| borders: { | |
| r: [0, 255], | |
| g: [0, 255], | |
| b: [0, 255], | |
| }, | |
| initials: { |
| function renderNextFrame(callback){ | |
| if(typeof callback !== "function") return; | |
| requestAnimationFrame(() => { | |
| requestAnimationFrame(() => { | |
| callback(); | |
| }); | |
| }); | |
| } |
| document.addEventListener("click", () => { | |
| setTimeout(() =>{ | |
| Promise.resolve(13).then((value) => console.log(value)); | |
| console.log("click1"); | |
| }) | |
| }) | |
| document.addEventListener("click", () => { | |
| setTimeout(() =>{ | |
| Promise.resolve(17).then((value) => console.log(value)); | |
| console.log("click2"); |
| function closestPoint(arr, point){ | |
| if(!Array.isArray(arr)) return []; | |
| point = Number.isFinite(point) ? point : 0; | |
| let prevDiff = Infinity; | |
| let result = arr.findIndex((value,key) => { | |
| let diff = Math.abs(value - point); | |
| return diff >= prevDiff ? true : (prevDiff = diff, false); | |
| }); | |
| result = result === -1 ? arr.length - 1 : result - 1; | |
| return {key: result, value: arr[result]}; |