Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果不使用',它是否会在内存中创建许多函数;新';js中的关键字?_Javascript_Vue.js - Fatal编程技术网

Javascript 如果不使用',它是否会在内存中创建许多函数;新';js中的关键字?

Javascript 如果不使用',它是否会在内存中创建许多函数;新';js中的关键字?,javascript,vue.js,Javascript,Vue.js,假设我有以下代码: function Human(firstName, lastName) { this.firstName = firstName, this.lastName = lastName, this.fullName = function() { return this.firstName + " " + this.lastName; } } var person1 = new Human("Virat", "Kohli"); va

假设我有以下代码:

function Human(firstName, lastName) {
    this.firstName = firstName,
    this.lastName = lastName,
    this.fullName = function() {
        return this.firstName + " " + this.lastName;
    }
}

var person1 = new Human("Virat", "Kohli");
var person2 = new Human("sddasd", "dasd");
这段代码所做的是针对每个创建的实例,在内存中一次又一次地创建这两个属性和一个方法(fullName),这是不好的,应该避免

现在我有了下面的代码。我有一个标记数组,我想给每个标记添加相同的函数(渲染函数)

for(let i=0;i<markers.length;i++){   
    markers[i].render = () => {
      let parentDiv = document.createElement('div');
      parentDiv.innerHTML = content;
      parentDiv.getElementsByClassName('openLocationClass')[0].onclick = () => openMarkerLocationPopupFunc(marker, data.workorder_id)
      if (showInstructions) parentDiv.getElementsByClassName('showInstructionsClass')[0].onclick = () => openShowInstructionPopupFunc(data.workorder_id)
      return parentDiv;
    }
}
for(设i=0;i{
让parentDiv=document.createElement('div');
parentDiv.innerHTML=内容;
parentDiv.getElementsByClassName('openLocationClass')[0]。onclick=()=>openMarkerLocationPopupFunc(标记,数据.workorder\u id)
if(showInstructions)parentDiv.getElementsByClassName('showInstructionClass')[0]。onclick=()=>openShowInstructionPopupFunc(data.workorder\u id)
返回parentDiv;
}
}
问题:

这会在内存中为每个标记创建相同函数的副本吗?或者这是如何工作的?如果有任何方法可以改进这一点的话?问题是,我还会在参数中传递两个函数,并将它们放到新创建的div的onclick中

这将为每个标记在内存中创建相同函数的副本

是的。现在,在任何一个像样的JavaScript引擎上,它们都会重用相同的底层代码,所以“只是”围绕该代码的额外函数对象被复制。但是,是的,存在重复。不过,这不一定是个问题

如果调用
渲染
时将
设置为
标记
,则可以很容易地避免重复:

function renderMarker() {
  let parentDiv = document.createElement('div');
  parentDiv.innerHTML = content;
  parentDiv.getElementsByClassName('openLocationClass')[0].onclick = () => openMarkerLocationPopupFunc(this, data.workorder_id);
  if (showInstructions) parentDiv.getElementsByClassName('showInstructionsClass')[0].onclick = showInstructionsClassClick;
  return parentDiv;
}

function showInstructionsClassClick() {
    openShowInstructionPopupFunc(data.workorder_id);
}

for(let i=0;i<markers.length;i++){   
    markers[i].render = renderMarker;
}
函数renderMarker(){
让parentDiv=document.createElement('div');
parentDiv.innerHTML=内容;
parentDiv.getElementsByClassName('openLocationClass')[0]。onclick=()=>openMarkerLocationPopupFunc(此为data.workorder_id);
if(showInstructions)parentDiv.getElementsByClassName('showInstructionClass')[0].onclick=showInstructionClassClick;
返回parentDiv;
}
函数显示说明ClassClick(){
openShowInstructionPopupFunc(数据.工作顺序\u id);
}

for(设i=0;问题是for循环中只有data.workorder_id。这就是为什么我认为我的解决方案工作得很好。如果我使用你的解决方案,renderMarker函数将不知道data.workorder_id。@NikaKurashvili-你没有在循环中设置
data
。上面的代码与你现有的循环在同一范围内,可以工作(因为当时
数据
在范围内)。是的,我错了。我在for循环中也有数据(每次都不同)。@NikaKurashvili-如果
数据
在每个循环中也不同,那么没有进一步的上下文,我无法立即找到避免创建函数的方法(可以直接执行,也可以通过
bind
创建函数)。