js Module
This module provides some JavaScript utilities. All members can be accessed with "cc.js".
Classes
Index
Properties
_registeredClassIdsUnknownAll classes registered in the engine, indexed by ID._registeredClassNamesUnknownAll classes registered in the engine, indexed by name.
Methods
isNumberIf a number is created by using 'new Number(10086)', the typeof it will be "object"...isStringCheck the obj whether is string or not.addonCopy all properties not defined in obj from arguments[1...n]mixincopy all properties from arguments[1...n] to objextendDerive the class from the supplied base class.getSuperGet super classisChildClassOfChecks whether subclass is child of superclass or equals to superclassclearRemoves all enumerable properties from objectisEmptyObjectChecks whether obj is an empty objectgetPropertyDescriptorGet property descriptor in object and all its ancestorsvalueDefine value, just help to call Object.defineProperty.
...getsetDefine get set accessor, just help to call Object.defineProperty(...)getDefine get accessor, just help to call Object.defineProperty(...)setDefine set accessor, just help to call Object.defineProperty(...)getClassNameGet class name of the object, if object is just a {} (and which class named 'Object'), it will return ""._setClassIdRegister the class by specified id, if its classname is not defined, the class name will also be set.setClassNameRegister the class by specified name manuallyunregisterClassUnregister a class from fireball._getClassByIdGet the registered class by idgetClassByNameGet the registered class by name_getClassIdGet class id of the objectobsoleteDefines a polyfill field for obsoleted codes.obsoletesDefines all polyfill fields for obsoleted codes corresponding to the enumerable properties of props.formatStrA string tool to construct a string with format string.createMapA simple wrapper ofObject.create(null)which ensures the return object have no prototype (and thus no inherited members).
Details
Properties
_registeredClassIds
All classes registered in the engine, indexed by ID.
| meta | description |
|---|---|
| Type | Unknown |
| Defined in | cocos2d/core/platform/js.js:446 |
Examples
// save all registered classes before loading scripts
let builtinClassIds = cc.js._registeredClassIds;
let builtinClassNames = cc.js._registeredClassNames;
// load some scripts that contain CCClass
...
// clear all loaded classes
cc.js._registeredClassIds = builtinClassIds;
cc.js._registeredClassNames = builtinClassNames;
_registeredClassNames
All classes registered in the engine, indexed by name.
| meta | description |
|---|---|
| Type | Unknown |
| Defined in | cocos2d/core/platform/js.js:462 |
Examples
// save all registered classes before loading scripts
let builtinClassIds = cc.js._registeredClassIds;
let builtinClassNames = cc.js._registeredClassNames;
// load some scripts that contain CCClass
...
// clear all loaded classes
cc.js._registeredClassIds = builtinClassIds;
cc.js._registeredClassNames = builtinClassNames;
Methods
isNumber
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.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/platform/js.js:58 |
Parameters
objAny
isString
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.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/platform/js.js:70 |
Parameters
objAny
addon
Copy all properties not defined in obj from arguments[1...n]
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/platform/js.js:82 |
Parameters
mixin
copy all properties from arguments[1...n] to obj
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/platform/js.js:109 |
Parameters
extend
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.
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/platform/js.js:134 |
Parameters
getSuper
Get super class
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/platform/js.js:168 |
Parameters
ctorFunction the constructor of subclass
isChildClassOf
Checks whether subclass is child of superclass or equals to superclass
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/platform/js.js:180 |
Parameters
clear
Removes all enumerable properties from object
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:215 |
Parameters
objAny
isEmptyObject
Checks whether obj is an empty object
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/platform/js.js:227 |
Parameters
objAny
getPropertyDescriptor
Get property descriptor in object and all its ancestors
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/platform/js.js:240 |
Parameters
value
Define value, just help to call Object.defineProperty.
The configurable will be true.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:258 |
Parameters
getset
Define get set accessor, just help to call Object.defineProperty(...)
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:282 |
Parameters
get
Define get accessor, just help to call Object.defineProperty(...)
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:312 |
Parameters
set
Define set accessor, just help to call Object.defineProperty(...)
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:335 |
Parameters
getClassName
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)
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/platform/js.js:352 |
Parameters
_setClassId
Register the class by specified id, if its classname is not defined, the class name will also be set.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:439 |
Parameters
setClassName
Register the class by specified name manually
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:478 |
Parameters
unregisterClass
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:495 |
Parameters
constructorFunction the class you will want to unregister, any number of classes can be added
_getClassById
Get the registered class by id
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/platform/js.js:518 |
Parameters
classIdString
getClassByName
Get the registered class by name
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/platform/js.js:529 |
Parameters
classnameString
_getClassId
Get class id of the object
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/platform/js.js:539 |
Parameters
obsolete
Defines a polyfill field for obsoleted codes.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:572 |
Parameters
objAny YourObject or YourClass.prototypeobsoletedString "OldParam" or "YourClass.OldParam"newExprString "NewParam" or "YourClass.NewParam"writableBoolean
obsoletes
Defines all polyfill fields for obsoleted codes corresponding to the enumerable properties of props.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/js.js:606 |
Parameters
objAny YourObject or YourClass.prototypeobjNameAny "YourObject" or "YourClass"propsObjectwritableBoolean
formatStr
A string tool to construct a string with format string.
| meta | description |
|---|---|
| Returns | String |
| Defined in | cocos2d/core/platform/js.js:624 |
Parameters
msgString | Any A JavaScript string containing zero or more substitution strings (%s).substAny JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.
Examples
cc.js.formatStr("a: %s, b: %s", a, b);
cc.js.formatStr(a, b, c);
createMap
A simple wrapper of Object.create(null) which ensures the return object have no prototype (and thus no inherited members). So we can skip hasOwnProperty calls on property lookups. It is a worthwhile optimization than the {} literal when hasOwnProperty calls are necessary.
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/platform/js.js:673 |
Parameters
forceDictModeBoolean Apply the delete operator to newly created map object. This causes V8 to put the object in "dictionary mode" and disables creation of hidden classes which are very expensive for objects that are constantly changing shape.