最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - implementation of angular.isBoolean? - Stack Overflow

programmeradmin3浏览0评论

I was reviewing some source code and underscore/lodash was included just for the _.isBoolean function. The underscore source is below:

_.isBoolean = function(obj) {
    return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
};

Looking at Function ponents in ng I see similar functions (angular.isObject, angular.isString, angular.isNumber, etc), but no angular.isBoolean function.

The angular.js source has this an internal function (source below), but an issue requesting to make public (feat: register isBoolean as a public member of global angular #5185) was closed saying "other libraries like underscore and lodash solve these problems well".

function isBoolean(value) {
  return typeof value === 'boolean';
}

Questions:

  • My initial reaction was to copy isBoolean and make a named function in my code, but which implementation is more correct?
  • Do I use the underscore version in anticipation of patibility with a future upgrade?
  • I assume it is a bad idea to "duck punch" my implementation into angular.isBoolean?

I was reviewing some source code and underscore/lodash was included just for the _.isBoolean function. The underscore source is below:

_.isBoolean = function(obj) {
    return obj === true || obj === false || toString.call(obj) == '[object Boolean]';
};

Looking at Function ponents in ng I see similar functions (angular.isObject, angular.isString, angular.isNumber, etc), but no angular.isBoolean function.

The angular.js source has this an internal function (source below), but an issue requesting to make public (feat: register isBoolean as a public member of global angular #5185) was closed saying "other libraries like underscore and lodash solve these problems well".

function isBoolean(value) {
  return typeof value === 'boolean';
}

Questions:

  • My initial reaction was to copy isBoolean and make a named function in my code, but which implementation is more correct?
  • Do I use the underscore version in anticipation of patibility with a future upgrade?
  • I assume it is a bad idea to "duck punch" my implementation into angular.isBoolean?
Share Improve this question edited Jul 2, 2014 at 17:19 Kevin Hakanson asked Jul 2, 2014 at 15:51 Kevin HakansonKevin Hakanson 42.2k23 gold badges129 silver badges158 bronze badges 11
  • What do you mean by "convert to a local function"? – Bergi Commented Jul 2, 2014 at 16:04
  • 1 Do you understand the differences between the functions? Then you should know that there is no "more correct" one. – Bergi Commented Jul 2, 2014 at 16:05
  • 1 @Ian Somebody must've. The check was added two years after the project started: github./jashkenas/underscore/mit/… :) I actually think I remember seeing Modernizr using them. – Brian Nickel Commented Jul 2, 2014 at 16:44
  • 1 @Ian github./Modernizr/Modernizr/… For properties. If true, the value has properties with specific details. Really any object would work (be truthy) but maybe they're dealing with consumers doing result == true. – Brian Nickel Commented Jul 2, 2014 at 16:54
  • 1 @Ian: When searching GitHub, I've only found it in a) test cases b) spec quotes :-) – Bergi Commented Jul 2, 2014 at 17:00
 |  Show 6 more ments

1 Answer 1

Reset to default 14

I was reviewing some source code and underscore/lodash was included just for the _.isBoolean function. […] My initial reaction was to convert isBoolean to a local function

Yes, good idea (if you emphasize the just). Maybe not even a function, but simply inline it.

but which implementation is more correct?

They behave differently when objects that are instances of the Boolean class are passed in. Will such ever occur in the app you are reviewing? Probably not. If they do, only you will know whether you want to consider them as booleans.

Apart from that, val === true || val === false has the same effect as typeof val == "boolean".

I assume it is a bad idea to "duck punch" my implementation into angular.isBoolean?

It's unlikely that angular will ever do this, so you hardly will provoke a collision. Still, ask yourself: Is it actually useful there? Will other code use it? For more discussion, have a look at Don't modify objects you don't own.

发布评论

评论列表(0)

  1. 暂无评论