Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

Module js

This module provides some JavaScript utilities. All members can be accessed with "cc.js".

Methods

isNumber
(
  • obj
)
Boolean

Check the obj whether is number or not If a number is created by using 'new Number(10086)', the typeof it will be "object"... Then you can use this function if you care about this case.

name type description
obj Any

returns:

type: Boolean

isString
(
  • obj
)
Boolean

Check the obj whether is string or not. If a string is created by using 'new String("blabla")', the typeof it will be "object"... Then you can use this function if you care about this case.

name type description
obj Any

returns:

type: Boolean

addon
(
  • obj
  • sourceObj
)
Object deprecated

This method is deprecated, use cc.js.mixin please.
Copy all properties not defined in obj from arguments[1...n]

name type description
obj Object

object to extend its properties

sourceObj Object

source object to copy properties from

returns:

type: Object

the result obj

mixin
(
  • obj
  • sourceObj
)
Object

copy all properties from arguments[1...n] to obj

name type description
obj Object
sourceObj Object

returns:

type: Object

the result obj

extend
(
  • cls
  • base
)
Function

Derive the class from the supplied base class. Both classes are just native javascript constructors, not created by cc.Class, so usually you will want to inherit using cc.Class instead.

name type description
cls Function
base Function

the baseclass to inherit

returns:

type: Function

the result class

getSuper
(
  • ctor
)
Function

Get super class

name type description
ctor Function

the constructor of subclass

returns:

type: Function

clear
(
  • obj
)

Removes all enumerable properties from object

name type description
obj Any

getPropertyDescriptor
(
  • obj
  • name
)
Object

Get property descriptor in object and all its ancestors

name type description
obj Object
name String

returns:

type: Object

value
(
  • obj
  • prop
  • value
  • [writable =false]
  • [enumerable =false]
)

Define value, just help to call Object.defineProperty.
The configurable will be true.

name type description
obj Object
prop String
value Any
writable optional Boolean false
enumerable optional Boolean false

getset
(
  • obj
  • prop
  • getter
  • setter
  • [enumerable =false]
)

Define get set accessor, just help to call Object.defineProperty(...)

name type description
obj Object
prop String
getter Function
setter Function
enumerable optional Boolean false

get
(
  • obj
  • prop
  • getter
  • [enumerable =false]
  • [configurable =false]
)

Define get accessor, just help to call Object.defineProperty(...)

name type description
obj Object
prop String
getter Function
enumerable optional Boolean false
configurable optional Boolean false

set
(
  • obj
  • prop
  • setter
  • [enumerable =false]
  • [configurable =false]
)

Define set accessor, just help to call Object.defineProperty(...)

name type description
obj Object
prop String
setter Function
enumerable optional Boolean false
configurable optional Boolean false

getClassName
(
  • objOrCtor
)
String

Get class name of the object, if object is just a {} (and which class named 'Object'), it will return "". (modified from the code from this stackoverflow post)

name type description
objOrCtor Object | Function

instance or constructor

returns:

type: String

_setClassId
(
  • classId
  • constructor
)
private

Register the class by specified id, if its classname is not defined, the class name will also be set.

name type description
classId String
constructor Function

setClassName
(
  • className
  • constructor
)

Register the class by specified name manually

name type description
className String
constructor Function

unregisterClass
(
  • constructor
)

Unregister a class from fireball.

If you dont need a registered class anymore, you should unregister the class so that Fireball will not keep its reference anymore. Please note that its still your responsibility to free other references to the class.

name type description
constructor Function

the class you will want to unregister, any number of classes can be added

_getClassById
(
  • classId
)
Function private

Get the registered class by id

name type description
classId String

returns:

type: Function

constructor

getClassByName
(
  • classname
)
Function

Get the registered class by name

name type description
classname String

returns:

type: Function

constructor

_getClassId
(
  • obj
  • [allowTempId =true]
)
String private

Get class id of the object

name type description
obj Object | Function

instance or constructor

allowTempId optional Boolean true

can return temp id in editor

returns:

type: String

obsolete
(
  • obj
  • obsoleted
  • newPropName
  • [writable =false]
)

Defines a polyfill field for obsoleted codes.

name type description
obj Any

YourObject or YourClass.prototype

obsoleted String

"OldParam" or "YourClass.OldParam"

newPropName String

"NewParam"

writable optional Boolean false

obsoletes
(
  • obj
  • objName
  • props
  • [writable =false]
)

Defines all polyfill fields for obsoleted codes corresponding to the enumerable properties of props.

name type description
obj Any

YourObject or YourClass.prototype

objName Any

"YourObject" or "YourClass"

props Object
writable optional Boolean false

formatStr ( ) String

A string tool to construct a string with format string. for example: cc.js.formatStr("a: %s, b: %s", a, b); cc.js.formatStr(a, b, c);

returns:

type: String