有个疑惑,以下两种写法在内存分配上会有什么区别?或者说应该用哪种才是正确的? 辛苦各位啦!
node.getComponent(cc.Sprite).spriteFrame = cc.SpriteFrame(url);
node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(url);
native没问题,browser下不用new会创建失败。
标准来说,要用new。不new的话代表的是一个类,new后代表这个类的一个实例。
new的意义和用法参考:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/new
1赞
不用 new,会引起内存泄露……
请问下不用new的话在chrome浏览器会报错是什么原因?
错误如下:
VM113:2 Uncaught TypeError: this.initProps is not a function(…)
(function cc_SpriteFrame(){
this.initProps(fireClass);
var cs=fireClass.ctors;
(cs[0]).apply(this,arguments);
(cs[1]).apply(this,arguments);
(cs[2]).apply(this,arguments);
(cs[3]).apply(this,arguments);
})
不用 new 的话,this 会变成 window,你的 window 上并没有定义这些成员变量,当然就会报错啦!奇怪的是你怎么就不用 new!就用一下下嘛!就用一下嘛!就用下嘛!用下嘛!用嘛!