Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to define a common Class in a file? #1304

Closed
xubing opened this issue Nov 21, 2016 · 1 comment
Closed

how to define a common Class in a file? #1304

xubing opened this issue Nov 21, 2016 · 1 comment

Comments

@xubing
Copy link

xubing commented Nov 21, 2016

我在查看文档http://www.cocos.com/docs/creator/scripting/class.html的时候,遇到一个问题。
下面是文档中的代码demo。
var Shape = cc.Class({
ctor: function () {
cc.log("Shape"); // 实例化时,父构造函数会自动调用,
}
});

var Square = cc.Class({
extends: Shape,
ctor: function () {
cc.log("Square"); // 再调用子构造函数
}
});

var square = new Square();
//以上代码将依次输出 "Shape" 和 "Square"。

//如果把上面的代码拆分成2个文件后调用,
//Shape.js
cc.Class({
ctor: function () {
cc.log("Shape"); // 实例化时,父构造函数会自动调用,
}
});

//Square.js
var Shape = require('Shape');
cc.Class({
extends: Shape,
ctor: function () {
cc.log("Square"); // 再调用子构造函数
}
});

//helloworld.js中调用
var Square = requir('Square');
var square = new Square();
//这个类会查询不到,然后报错。如果我现在让Shape类继承于cc.Component后,就可以了。
//Component 是特殊的 CCClass,不能定义构造函数,它的构造职能可由 onLoad 方法代替。我现在的初衷只是定义一个常见的类,不需要继承Component。

@xubing
Copy link
Author

xubing commented Nov 21, 2016

ok. 用module.exports导出就ok了。

@xubing xubing closed this as completed Nov 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant