这个javascript原型模式有什么好处?(What is the benefit of this javascript prototype pattern?)
我看到一些代码以这种方式定义原型中的方法:
function Class() {...} (function() { this.method1 = function() {...}; this.method2 = function() {...}; }).call(Class.prototype);
这种结构与其他结构相比有什么好处? 以下是其他一些例子:
function Class() {...} Class.prototype = { method1: function() {...}, method2: function() {...} };
要么
function Class() {...} Class.prototype.method1 = function() {...}; Class.prototype.method2: function() {...};
要么
function Class() {...} Class.prototype = Object.create({...}, {...}); Class.prototype.constructor = Class;
以下我知道在内存方面效率稍低,因为每个实例都有自己的method1和method2副本:
function Class() { var privateVar = 0; this.method1 = function() {...}; this.method2 = function() {...}; }
当然,这种结构的好处是方法可以访问私有变量。
I saw some code defines the methods in the prototype this way:
function Class() {...} (function() { this.method1 = function() {...}; this.method2 = function() {...}; }).call(Class.prototype);
What is the benefit of this construct versus others? Here are some other examples:
function Class() {...} Class.prototype = { method1: function() {...}, method2: function() {...} };
or
function Class() {...} Class.prototype.method1 = function() {...}; Class.prototype.method2: function() {...};
or
function Class() {...} Class.prototype = Object.create({...}, {...}); Class.prototype.constructor = Class;
the following I know is a bit less efficient in term of memory because each instance has its own copy of method1 and method2:
function Class() { var privateVar = 0; this.method1 = function() {...}; this.method2 = function() {...}; }
of course the benefit of this construct is that the methods can access private variables.
原文:https://stackoverflow.com/questions/23556185
最满意答案
问题是您正在尝试将文本元素附加到圆圈。
var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("circle") .attr("class", "node") .attr("r", function(d) { return d.degree; }) .style("fill", function(d) { return color(d.group); }) .call(force.drag); // node here consists of svg circles. node.append("text") .text(function(d) { return d.name; })
试试这个
var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("g") .attr("class", "node") .call(force.drag); // moved this here node.append("circle") .attr("r", function(d) { return d.degree; }) .style("fill", function(d) { return color(d.group); }) // .call(force.drag); // node here is a `g` element so we can append text elements to it. node.append("text") .text(function(d) { return d.name; })
我还将
call(force.drag)
移到.attr("class", "node")
行之后,以便将它应用于给定node
元素的所有子node
。The issue is that you're trying to append a text element to a circle.
var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("circle") .attr("class", "node") .attr("r", function(d) { return d.degree; }) .style("fill", function(d) { return color(d.group); }) .call(force.drag); // node here consists of svg circles. node.append("text") .text(function(d) { return d.name; })
Try this instead
var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("g") .attr("class", "node") .call(force.drag); // moved this here node.append("circle") .attr("r", function(d) { return d.degree; }) .style("fill", function(d) { return color(d.group); }) // .call(force.drag); // node here is a `g` element so we can append text elements to it. node.append("text") .text(function(d) { return d.name; })
I also moved the
call(force.drag)
up to after the.attr("class", "node")
line in order to have it apply to all children of a givennode
element.
相关问答
更多-
用于学校教学的电脑网络教室,它的网络类型属于() A.互联网 B.广域网 C.城域网 D.局域网[2023-01-12]
d -
标记D3中的网络节点(Labeling network nodes in D3)[2022-08-05]
问题是您正在尝试将文本元素附加到圆圈。 var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("circle") .attr("class", "node") .attr("r", function(d) { return d.degree; }) .style("fill", function(d) { return color(d.group); }) ... -
好吧,似乎写了所有真正帮助我思考的东西,我自己设法解决了这个问题。 在这里发布答案以防万一有人碰巧遇到同样的问题。 非常简单的解决方案与执行.call(d3.drag())的对象有关。 我从节点变量( circle SVG元素)中剪切了这段代码,并将其粘贴到定义g SVG元素的变量的末尾: .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); 所以g元素声明最终 ...
-
经过几天的搜索后,我可以发布工作代码作为答案。 var actions = [ {"action":"arrivee","id":"001","service":1}, {"action":"arrivee","id":"002","service":1}, {"action":"arrivee","id":"003","service":1}, {"action":"arrivee","id":"004","service":1}, {"action":"arriv ...
-
首先,如果要在特定时间“冻结”图形,可以使用force布局的停止命令: force.stop() 一个很好的用法是首先让图形自组织(使用tick ),然后停止力: // include in beginning of script force.start(); for (var i = 0; i < n; ++i) force.tick(); force.stop(); 然后,如果你想拖放节点,一个好主意就是在d3示例页面上搜索drag ,你会发现以下链接: 拖放支持将节点设置为固定位置,当你被删除时 ...
-
我最初把它写成评论,但决定把它变成答案...... 看起来d3 v4将包含您想要的功能 。 如果您不想等待,可以立即窃取实施并将其添加到选择原型中: d3.selection.prototype.nodes = function(){ var nodes = new Array(this.size()), i = -1; this.each(function() { nodes[++i] = this; }); return nodes; } 用法示例: d3.selection.p ...
-
有一些不太明显的力量在起作用: 初始化时,原始数据阵列中的节点获得一堆新属性 初始化后,原始数据数组中的链接将转换为新形式。 此外,如果您正在关注链接示例,则不需要输入/更新/退出样式循环 - 强制使用原始数据数组,而不是任何DOM元素。 节点 在您的参考示例中,节点仅以id和组开头: {"source": "Montparnasse", "target": "Babet", "value": 2} 但是,在使用simulation.nodes(nodes)初始化之后,它们会获得一些附加参数: group ...
-
您不能将
元素附加到 元素。 这里的惯用解决方案是将node转换为组选择,就像Mike Bostock在您分享的示例中一样: node.enter().insert("g", ".cursor") .attr("class", "node"); 然后,将圆圈和文本附加到node : node.append("circle") .attr("r", 10) .on("mousedown", mousedownNode); node.append("text" ... -
D3文档提供了链接如何工作的解释: https://github.com/mbostock/d3/wiki/Force-Layout#wiki-links 简而言之,链接数组可以包含source和target索引,它们可以从nodes重新映射到对象,也可以包含从nodes本身引用的对象。 您需要将链接的source和target重新映射到nodes的对象。 假设您的source和target属性使用上面第二个示例中显示的名称,则可以将以下代码片段添加到d3.json回调的开始d3.json执行重新映射: ...
-
如果要创建强制定向图表,可以使用forceX和forceY排列屏幕中的节点。 根据API : x和y定位力以可配置的强度将节点推向沿给定尺寸的期望位置。 力的强度与节点位置和目标位置之间的一维距离成比例。 在这个演示中,我将根据location获取数据数组并定位在x坐标中。 首先,我设定了一个比例: var xScale = d3.scalePoint() .domain([1, 2, 3, 4]) .range([100, width - 100]); 并在forceX使用此比例: va ...