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

javascript - What is the shorthand in d3 for the identity function ("function(d) { return d; }")? - Stack Over

programmeradmin1浏览0评论

Looking through the d3 docs, I see this code (the identity function) repeated everywhere:

function(d) { return d; }

Is there a built-in way in d3 to do this? I know I could create my own no-op identity function and use it everywhere, but it seems like d3 should provide this.

Looking through the d3 docs, I see this code (the identity function) repeated everywhere:

function(d) { return d; }

Is there a built-in way in d3 to do this? I know I could create my own no-op identity function and use it everywhere, but it seems like d3 should provide this.

Share Improve this question edited Jun 22, 2013 at 13:58 Lukas asked Jun 22, 2013 at 13:41 LukasLukas 9,8452 gold badges39 silver badges45 bronze badges 4
  • 1 The Functional library has a method called "K" that, given a value, returns a function that returns that value. That library is written as an exercise in providing a functional programming platform, however, so it's kind-of odd. – Pointy Commented Jun 22, 2013 at 13:46
  • Hmm. Interesting. I'd prefer to stick to something that's provided by d3, if possible. – Lukas Commented Jun 22, 2013 at 13:57
  • D3 doesn't provide this. – Lars Kotthoff Commented Jun 22, 2013 at 15:22
  • Also, Underscore provides _.identity. – Lukas Commented Oct 15, 2013 at 15:27
Add a ment  | 

2 Answers 2

Reset to default 4

I was wondering why there wasn't a d3.identity function as part of the library, and couldn't find a reason not to have one.

From a performance point of view, defining an identity function gives better performance than reusing the Object constructor. It makes little difference if you reuse the same identity function across different types. Some performance tests are here.

So in my case I abuse D3 and added the function myself:

d3.identity = function(d) { return d; }

If you're using underscore then you can also use the _.identity function.

Regarding using the Object constructor, my assumption is that this creates a new, unnecessary object each time it's called which wastes memory and CPU time, both for creation and garbage collection. This may be optimised away for immutable types such as numbers in some runtimes.

EDIT Phrogz has a brief article showing some useful shorthand for reducing the number of lambdas when working with D3, that includes an identity function.

I used to see Mike do .data(Object) which seems to work

http://tributary.io/inlet/5842519

but I'm not sure why I don't see it around anymore

var svg = d3.select("svg")

var data = [[10,20],[30,40]];
svg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("transform", function(d,i) { return "translate(" + [i * 100, 0] + ")"})
.selectAll("circle")
//.data(function(d) { console.log(d); return d })
.data(Object)
.enter()
.append("circle")
.attr({
  cx: function(d,i) { return 100 + i * 40 },
  cy: 100,
  r: function(d,i) { return d }
})

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论