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:

  1. import {
  2. Component, // Import class Component
  3. _decorator, // mport namespace _decorator
  4. } from 'cc';
  5. import * as modules from 'cc'; // Import the entire Cocos Creator module as a namespace Cocos Creator
  6. @_decorator.ccclass("MyComponent")
  7. export class MyComponent extends Component {
  8. public v = new cc.Vec3();
  9. }

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:

  1. /* const cc = {}; // Every Cocos Creator script is equivalent to an implicit definition here */
  2. import * as modules from 'cc'; // Error: Namespace import name cc is reserved by Cocos Creator
  3. const cc = { x: 0 };
  4. console.log(cc.x); // Error: The global object name cc is reserved by Cocos Creator
  5. function f () {
  6. const cc = { x: 0 };
  7. console.log(cc.x); // Correct: cc can be used as the name of a local object
  8. const o = { cc: 0 };
  9. console.log(o.cc); // Correct: cc can be used as a property name
  10. }
  11. console.log(cc, typeof cc); // error: behavior is undefined