DivMan Posted January 8, 2016 Report Share Posted January 8, 2016 Он сравнивает всегда с true? var d = true; !d d не true = false var a = false; ! aa не true = true Quote Link to comment Share on other sites More sharing options...
0 Нарек Posted January 8, 2016 Report Share Posted January 8, 2016 Он берет переменную, приводит его в boolean тип и возвращает обратное. Quote Link to comment Share on other sites More sharing options...
0 DivMan Posted January 9, 2016 Author Report Share Posted January 9, 2016 Он берет переменную, приводит его в boolean тип и возвращает обратное.Он всегда приводит к true и с ним сравнивает? Quote Link to comment Share on other sites More sharing options...
0 wwt Posted January 9, 2016 Report Share Posted January 9, 2016 Он всегда приводит к true и с ним сравнивает? Он приводит к логическому типу (boolen) и меняет на обратное К примеру:!0;//приводит к логическому false и меняет на обратное, вернет true!1;//приводит к логическому true и меняет на обратное, вернет false Quote Link to comment Share on other sites More sharing options...
0 Sensei Posted January 9, 2016 Report Share Posted January 9, 2016 По спецификации алгоритм такой- преобразовываем выражение к значению Boolean- если результат равен true возвращаем false- возвращаем trueМожно представить в виде функцииvar not = function(expr) { expr = !!expr; if (expr === true) return false; return true; };not(1 > 2); // truenot(1 < 2); // false Quote Link to comment Share on other sites More sharing options...
0 DivMan Posted January 9, 2016 Author Report Share Posted January 9, 2016 (edited) По спецификации алгоритм такой- преобразовываем выражение к значению Boolean- если результат равен true возвращаем false- возвращаем trueМожно представить в виде функцииvar not = function(expr) { expr = !!expr; if (expr === true) return false; return true; };not(1 > 2); // truenot(1 < 2); // false А как переменная получает значение у само самой? expr = !!expr; Edited January 9, 2016 by DivMan Quote Link to comment Share on other sites More sharing options...
0 wwt Posted January 9, 2016 Report Share Posted January 9, 2016 А как переменная получает значение у само самой? expr = !!expr; Quote Link to comment Share on other sites More sharing options...
0 DivMan Posted January 10, 2016 Author Report Share Posted January 10, 2016 А как переменная получает значение у само самой? expr = !!expr; Или это параметр? Quote Link to comment Share on other sites More sharing options...
0 abrahadabra Posted January 10, 2016 Report Share Posted January 10, 2016 А как переменная получает значение у само самой? expr = !!expr;это двойное отрицание. Фактически приведение к булеву типу. При отрициании производится приведение типа автоматом, а при повторном отрицании возвращается булево значение исходной переменной. Quote Link to comment Share on other sites More sharing options...
0 wwt Posted January 10, 2016 Report Share Posted January 10, 2016 Или это параметр? Выражение !!expr (двойное логическое отрицание) является аналогом Boolean(expr) , оба действия это явное приведение к логическому типу. https://learn.javascript.ru/types-conversion Quote Link to comment Share on other sites More sharing options...
Question
DivMan
Он сравнивает всегда с true?
var d = true;
!d
d не true = false
var a = false;
! a
a не true = true
Link to comment
Share on other sites
9 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.