- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
function saveQuestion() {
var questionContent = {};
var questionResult = {};
switch(questionFields.attr('class')) {
case 'check-question':
questionResult.text = questionFields.children('.question-text')[0].innerText;
questionResult.type = 'check';
questionResult.answers = [];
[].forEach.call(questionFields.children('.answer-preview'), function(answerElement, i, arr) {
var answer = {};
answer.text = $(answerElement).children('.answer-text')[0].innerText;
answer.right = ($(answerElement).children('.answer-check')[0].checked) ? 1 : 0;
answer.weight = (!answer.right) ? $(answerElement).children('.answer-weight')[0].getPosition() : 1;
answer.weight = (answer.weight > 0 && answer.weight <= 1) ? answer.weight : 0;
questionResult.answers.push(answer);
});
if (checkQuestionCorrect(questionResult)) {
questionResult= JSON.stringify(questionResult);
questionContent = JSON.parse(questionResult); //клонируем объект
[].forEach.call(questionContent.answers, function(answer, i, answers) { delete answer.right; delete answer.weight; });
questionContent = JSON.stringify(questionContent);
console.log('result: ' + questionResult);
console.log('content: ' + questionContent);
net.addQuestion(loID, questionContent, questionResult, function(r){
$('.add-question').slideUp(200, function(){
$('.add-question-row').remove();
openLOPreview(loID);
});
});
}
break;
case 'input-question':
var highlights = highlighter.highlights;
questionResult.type = 'input';
questionResult.text = $('#question-text-area').get(0).innerText;
questionResult.answers = [];
for (i = 0; i < highlights.length; i++) {
var answer = {};
answer.id = highlights[i].id;
answer.posStart = highlights[i].characterRange.start;
answer.posEnd = highlights[i].characterRange.end;
answer.text = highlights[i].answerText;
answer.strict = ('strict' in highlights[i]) ? highlights[i].strict : true;
questionResult.answers.push(answer);
}
questionResult.answers.sort(function(a, b){ return a.posStart - b.posStart; });
questionResult.serializedHighlight = highlighter.serialize();
questionResult = JSON.stringify(questionResult);
questionContent = JSON.stringify(questionContent);
net.addQuestion(loID, questionContent, questionResult, function(r){
$('.add-question').slideUp(200, function(){
$('.add-question-row').remove();
openLOPreview(loID);
});
});
break;
default: break;
}
}
Моя дипломная работа по теме "тестирование студентов". Конструктор тестов, обработчик кнопки сохранения вопроса. Используются библиотеки jQuery и Rangy (для работы с выделением текста).
Штоэта?
//Кэп, блядь.
В ГК же вызова нет: мы получаем ссылку на метод forEach, а для него делаем call с непустым массивом.
Короче, вот это вот: эквивалентно следующему:
Кэп.
В питоне тоже можно.
Но! Так уже нельзя.
А вот так уже нет:
obj.__class__ - получаем класс объекта.
И так тоже:
А можно как-нибудь от "".split отвязать аргумент ""?
Можно даже круче сделать,
[code language=python]
def unbind(method):
return method.__self__.__class__.__dict__[method.__name__]
[/code]
Шо ты кипятишься, как агицын паровоз?
Для разнообразия, в JS есть несколько коллекций, которые очень похожи на массивы (есть 0, 1, 2, ..., length), но таковыми не являются. Например, NodeList, Arguments. В случае, если эти коллекции не требуется менять, можно вполне рассматривать их как обычные массивы, но forEach, map, filter у них нет, потому приходится заимствовать их у обычного массива.
А в JS методы - те же функции, которые иногда используют неявный аргумент this. Работает утиная типизация. Если this имеет структуру, которую требует функция, то никто ничего не заметит.
Если метод не упоминает свой this, его спокойно можно брать у объекта и использовать как обычную функцию. Привязывать объект к функции нужно явно.
http://javascript.ru/function/call
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/call
Ещё есть bind и apply.