我正在编写一个模块,使用自定义过滤器,我发现了一些奇怪的东西.如果我console.log()在过滤器内部使用,它会将值记录两次,即使我只调用一次.有没有办法只记录一次?这是否意味着过滤器内的代码会被执行两次?
这是过滤器:
.filter('arrayToSentence', function($sce, $rootScope) {
return function(array, index) {
console.log(index);
var i, word, sentence = '<span style="color:red;">';
for (i = 0; i < array.length; i++) {
if (i < array.length - 1) {
sentence += array[i] + '&bnsp;';
} else {
sentence += array[i];
}
}
sentence = sentence += '</span>'
return $sce.sentence;
}
})
Run Code Online (Sandbox Code Playgroud)
将console.log(index)被记录两次.我需要确保我的滤波器逻辑不会在任何地方重复,因为我需要比较两个数组(一个被过滤,另一个用于对它们之间的差异进行着色,如缺少单词或单词不匹配).
[编辑]这是向我指出,我的问题是重复的,这 可是原来的问题没有回答如何避免这个问题,但我相信@defaultcheckbox提供充实的答案.
我正在写一个小程序,应该从一个正方形的底座创建不同形状的房间.形状应该从正方形继承,其中包含顶点的List.
Room (父类)代码:
class Room
{
private List<Point> vertices;
private int oX;
private int oY;
private int width;
private int height;
public Room(int oX, int oY, int width, int height)
{
vertices = new List<Point>();
addVertice(new Point(oX, oY));
addVertice(new Point(oX + width, oY));
addVertice(new Point(oX, oY + height));
addVertice(new Point(oX + width, oY + height));
this.width = width;
this.height = height;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我继承LShape从Room,但在Visual Studio说,vertices变量不能由于其保护级别访问.
LShape(儿童)班的代码
class LShape: Room
{
public LShape(int oX, …Run Code Online (Sandbox Code Playgroud) 我最近在java中使用过代码,遇到了这个问题,构造函数中的代码似乎没有被执行,因为编译器会抛出NullPointerException.
public class ObjectA {
protected static ObjectA oa;
private String message = "The message";
public ObjectA() {
oa = new ObjectA();
}
public static void main(String args[]) {
System.out.println(oa.message);
} }
Run Code Online (Sandbox Code Playgroud)
现在,当我在构造函数之前移动对象的创建时,即我在一行中完成它,然后一切正常.
任何人都可以向我解释为什么会发生这种情况,以及我对代码的理解是错误的?
提前致谢.
angularjs ×1
c# ×1
constructor ×1
creation ×1
execution ×1
inheritance ×1
java ×1
javascript ×1
object ×1