Skip to content

Instantly share code, notes, and snippets.

@dmitryt
Last active December 28, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryt/7536676 to your computer and use it in GitHub Desktop.
Save dmitryt/7536676 to your computer and use it in GitHub Desktop.
Задача №1
Раньше в JavaScript’e очень не хватало биндинга событий\функций. Начиная с Javascript 1.8.5(IE9, FF4, Chrome7, Opera 11.6) появилась нативная реализация. Раньше же приходилось писать собственную реализацию биндинга или использовать существующие решения.
Напишите реализацию функции bind, которая позволяет выполнять функцию в передаваемом контексте выполнения и аргументами.
Подсказка: Обратите внимание на методы apply/call нативного JavaScript.
Пример:
var App = function(){
return {
init: function() {
this.nodes = document.querySelectorAll('.node');
this.setListeners();
},
setListeners: function() {
[].slice.call(this.nodes).forEach(function(n){
n.onclick = this.onClick.myBind(this);
}, this);
},
onClick: function(e) {
e = e || window.event;
var node = e.target || e.srcElement;
// this - should be the main context - instance of App
// node - should be the node, that fires event
}
};
};
if (typeof Function.prototype.myBind !== 'undefined') {
Function.prototype.myBind = function(context){
// Put your code here
};
}
(new App()).init();
Задача №2
Напишите реализацию конструктора, принимающего на вход объект и создающего аттрибуты\методы по ключам этого объекта:
var Person = function(args){
// put your code here
};
var p = new Person({
name: ‘Jack’,
age: ’10’,
jump: function(){ return “My name is ” + this.name + “ and I can jump.”;}
});
p.name // ‘Jack’
p.age // 10
console.log(p.jump()) // “My name is Jack and I can jump.”
Задача №3
Модифицируйте конструктор из прошлой задачи, добавив к нему геттеры\сеттеры для каждого переданного свойства.
var p = new Person({
name: ‘Jack’,
age: ’10’
});
p.getName() // ‘Jack’
p.name // undefined
p.getAge() // 10
p.age // undefined
console.log(p.jump()) // “My name is Jack and I can jump.”
console.log(p.getJump) // undefined
@sudodoki
Copy link

if (typeof Function.prototype.myBind !== 'undefined') {
!== менять на ===

@sudodoki
Copy link

Модифицируйте конструктор из прошлой задачи == напишите новый и забейте на старый
Предполагайте, что в задании 3 вместо
Person фигурирует новый констурктор PersonExtended, например.

@sejoker
Copy link

sejoker commented Nov 25, 2013

Пример использования метода myBind из книги "Javascript design patterns":

var one = {
name: 'one',
say: function (greet) {
return console.log(greet + ', ' + this.name);
}
};

var two = {
name: 'two'
};

if (typeof Function.prototype.myBind === 'undefined' ){
//put your code here
}

var twoSay2 = one.say.myBind(two);
twoSay2('privet'); //privet, two

var twoSay3 = one.say.myBind(two, 'nihao');
twoSay3(); // nihao, two

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment