score:1

The property method

d3_selectionPrototype.property = function (name, value) {
    if (arguments.length < 2) {
        if (typeof name === "string") return this.node()[name];
        for (value in name) this.each(d3_selection_property(value, name[value]));
        return this;
    }
    return this.each(d3_selection_property(name, value)); 
}; 

When you pass in the value undefined, since you've passed in 2 arguments, it calls d3_selection_property (for each entry in the selection) which is

function d3_selection_property(name, value) {
    function propertyNull() {
        delete this[name];
    }
    function propertyConstant() {
        this[name] = value;
    }
    function propertyFunction() {
        var x = value.apply(this, arguments);
        if (x == null) delete this[name]; else this[name] = x;
    }
    return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant; }

The non-strict comparison (last line) value == null evaluates to true when value === undefined and so propertyNull is called doing this

delete this[name];

However, since the checked property is not actually on the this object (it's actually one level up the prototype chain of the DOM element), this actually does nothing.


So to sum up, it returns the selection (as all chainable d3 methods do), but it doesn't modify the actual checked value.


As per the documentation - https://github.com/mbostock/d3/wiki/Selections

If value is specified, sets the property with the specified name to the specified value on all selected elements.

but as per the current code, that won't happen if the specified value is undefined


Related Query