Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

javascript - 如何在不复制数组的情况下创建对现有数组的新引用(How to create new reference to existing array without copying the array)

Is it possible to create a new reference to an array without going through the array?

(是否可以不通过数组就创建对数组的新引用?)

My problem is that I have a pure angular pipe which doesn't detect push/pop changes.

(我的问题是我有一个纯角管,无法检测到推/弹出变化。)

I would like to avoid solutions like this:

(我想避免这样的解决方案:)

this.array = this.array.filter(e=>true)

Which adds complexity just to update reference.

(只是为了更新参考而增加了复杂性。)

I have tried first thing that came to my head but it doesn't work (pipe doesn't detect any changes) and I am not familiar with js/ts enough to know why it didn't work.

(我已经尝试过第一件事,但它不起作用(管道无法检测到任何更改),而且我对js / ts的熟悉程度不足以知道为什么它不起作用。)

const newRef = this.array;
this.array = null;
this.array = newRef

I have pipe which gets array of objects and array of filters and returns array of filtered objects.

(我有管道,它获取对象数组和过滤器数组并返回过滤对象的数组。)

@Pipe({
  name: 'eventFilter'
})
export class EventFilterPipe implements PipeTransform {

  transform(events: EventDtos[], filters:Filter[]): any {
     //return filtered events
  }

Pipe useage:

(管道用途:)

<div  class="event" *ngFor="let event of events  | eventFilter:filters">
   html stuff
</div>

After pushing/poping filter from filters , pipe's transform isn't called so I am using this code to force transform call:

(从filters推送/弹出过滤filters ,未调用管道的转换,因此我使用以下代码强制进行transform调用:)

this.filters = this.filters.filter(e=>true)

But at this point I don't know which one is faster, this method or impure pipe.

(但是目前我不知道哪种方法更快,这种方法还是不纯管道。)

So Ideally I would like to leave pure pipe and update filters reference without adding complexity

(因此,理想情况下,我想保留纯管道并更新filters参考,而不增加复杂性)

  ask by askstackoverflow translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you talking about code within <script> tags, or in the HTML tag attributes, like this?

(您是在谈论<script>标签内的代码,还是HTML标签属性中的代码?)

<a href="#" onclick="alert('this is inline JS');return false;">Click</a>

Either way, the debugger keyword like this will work:

(无论哪种方式,像这样debugger关键字将起作用:)

<a href="#" onclick="debugger; alert('this is inline JS');return false;">Click</a>

NB Chrome won't pause at debugger s if the dev tools are not open.

(NB Chrome不会在暂停debugger ■如果开发者工具都打不开。)


You can also set property breakpoints in JS files and <script> tags:

(您还可以在JS文件和<script>标记中设置属性断点:)

  1. Click the Sources tab

    (单击“ 源”选项卡)

  2. Click the Show Navigator icon and select the a file

    (单击“ 显示导航器”图标,然后选择一个文件)

  3. Double-click the a line number in the left-hand margin.

    (双击左侧边距中的行号。)

    A corresponding row is added to the Breakpoints panel (4).

    (相应的行将添加到“ 断点”面板(4)。)

在此输入图像描述


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...