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

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

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 }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745380602a4625206.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信