Operating environment
The APIs for the Cocos Creator engine all exist in the module cc
. They can be imported it using standard ES6 module import syntax. Example:
import {
Component, // Import class Component
_decorator, // mport namespace _decorator
} from "cc";
import * as cc from "cc"; // Import the entire Cocos Creator module as a namespace Cocos Creator
@_decorator.ccclass("MyComponent")
export class MyComponent extends Component {
public v = new cc.Vec3();
}
Reserved identifier cc
Due to historical reasons, cc
is an identifier reserved for Cocos Creator. Its behavior is equivalent to having defined an object named cc
at the top of any module. Therefore, developers should not use cc
as the name of any global object. Example:
/* const cc = {}; // Every Cocos Creator script is equivalent to an implicit definition here */
import * as cc from "cc"; // Error: Namespace import name cc is reserved by Cocos Creator
const cc = { x: 0 };
console.log(cc.x); // Error: The global object name cc is reserved by Cocos Creator
function f () {
const cc = { x: 0 };
console.log(cc.x); // Correct: cc can be used as the name of a local object
const o = { cc: 0 };
console.log(o.cc); // Correct: cc can be used as a property name
}
console.log(cc, typeof cc); // error: behavior is undefined