首页 \ 问答 \ 这个javascript原型模式有什么好处?(What is the benefit of this javascript prototype pattern?)

这个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
更新时间:2023-05-29 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(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 given node element.

相关问答

更多
  • 问题是您正在尝试将文本元素附加到圆圈。 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。