js
模块
这个模块封装了 JavaScript 相关的一些实用函数,你可以通过 cc.js
来访问这个模块。
类型
索引
属性(properties)
_registeredClassIds
Unknown
引擎中已注册的所有类型,通过 ID 进行索引。_registeredClassNames
Unknown
引擎中已注册的所有类型,通过名称进行索引。
方法
isNumber
Check the obj whether is number or notisString
Check the obj whether is string or not....addon
Copy all properties not defined in obj from arguments[1...n]mixin
copy all properties from arguments[1...n] to objextend
Both classes are just native javascript constructors, not created by cc.Class, sogetSuper
Get super classisChildClassOf
Checks whether subclass is child of superclass or equals to superclassclear
Removes all enumerable properties from objectisEmptyObject
Checks whether obj is an empty objectgetPropertyDescriptor
Get property descriptor in object and all its ancestorsvalue
Define value, just help to call Object.defineProperty.
...getset
Define get set accessor, just help to call Object.defineProperty(...)get
Define get accessor, just help to call Object.defineProperty(...)set
Define set accessor, just help to call Object.defineProperty(...)getClassName
Get class name of the object, if object is just a {} (and which class named 'Object'), it will return ""...._setClassId
Register the class by specified id, if its classname is not defined, the class name will also be set.setClassName
Register the class by specified name manuallyunregisterClass
Unregister a class from fireball...._getClassById
Get the registered class by idgetClassByName
Get the registered class by name_getClassId
Get class id of the objectobsolete
Defines a polyfill field for deprecated codes.obsoletes
Defines all polyfill fields for obsoleted codes corresponding to the enumerable properties of props.formatStr
A string tool to construct a string with format string.createMap
该方法是对Object.create(null)
的简单封装。
Details
属性(properties)
_registeredClassIds
引擎中已注册的所有类型,通过 ID 进行索引。
meta | description |
---|---|
类型 | Unknown |
定义于 | cocos2d/core/platform/js.js:446 |
示例
// 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
引擎中已注册的所有类型,通过名称进行索引。
meta | description |
---|---|
类型 | Unknown |
定义于 | cocos2d/core/platform/js.js:462 |
示例
// 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;
方法
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 |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/platform/js.js:58 |
参数列表
obj
Any
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 |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/platform/js.js:70 |
参数列表
obj
Any
addon
Copy all properties not defined in obj from arguments[1...n]
meta | description |
---|---|
返回 | Object |
定义于 | cocos2d/core/platform/js.js:82 |
参数列表
mixin
copy all properties from arguments[1...n] to obj
meta | description |
---|---|
返回 | Object |
定义于 | cocos2d/core/platform/js.js:109 |
参数列表
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 |
---|---|
返回 | Function |
定义于 | cocos2d/core/platform/js.js:134 |
参数列表
getSuper
Get super class
meta | description |
---|---|
返回 | Function |
定义于 | cocos2d/core/platform/js.js:168 |
参数列表
ctor
Function the constructor of subclass
isChildClassOf
Checks whether subclass is child of superclass or equals to superclass
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/platform/js.js:180 |
参数列表
clear
Removes all enumerable properties from object
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:215 |
参数列表
obj
Any
isEmptyObject
Checks whether obj is an empty object
meta | description |
---|---|
返回 | Boolean |
定义于 | cocos2d/core/platform/js.js:227 |
参数列表
obj
Any
getPropertyDescriptor
Get property descriptor in object and all its ancestors
meta | description |
---|---|
返回 | Object |
定义于 | cocos2d/core/platform/js.js:240 |
参数列表
value
Define value, just help to call Object.defineProperty.
The configurable will be true.
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:258 |
参数列表
getset
Define get set accessor, just help to call Object.defineProperty(...)
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:282 |
参数列表
get
Define get accessor, just help to call Object.defineProperty(...)
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:312 |
参数列表
set
Define set accessor, just help to call Object.defineProperty(...)
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:335 |
参数列表
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 |
---|---|
返回 | String |
定义于 | cocos2d/core/platform/js.js:352 |
参数列表
_setClassId
Register the class by specified id, if its classname is not defined, the class name will also be set.
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:439 |
参数列表
setClassName
Register the class by specified name manually
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:478 |
参数列表
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 |
---|---|
定义于 | cocos2d/core/platform/js.js:495 |
参数列表
constructor
Function the class you will want to unregister, any number of classes can be added
_getClassById
Get the registered class by id
meta | description |
---|---|
返回 | Function |
定义于 | cocos2d/core/platform/js.js:518 |
参数列表
classId
String
getClassByName
Get the registered class by name
meta | description |
---|---|
返回 | Function |
定义于 | cocos2d/core/platform/js.js:529 |
参数列表
classname
String
_getClassId
Get class id of the object
meta | description |
---|---|
返回 | String |
定义于 | cocos2d/core/platform/js.js:539 |
参数列表
obsolete
Defines a polyfill field for deprecated codes.
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:572 |
参数列表
obj
Any YourObject or YourClass.prototypeobsoleted
String "OldParam" or "YourClass.OldParam"newExpr
String "NewParam" or "YourClass.NewParam"writable
Boolean
obsoletes
Defines all polyfill fields for obsoleted codes corresponding to the enumerable properties of props.
meta | description |
---|---|
定义于 | cocos2d/core/platform/js.js:606 |
参数列表
obj
Any YourObject or YourClass.prototypeobjName
Any "YourObject" or "YourClass"props
Objectwritable
Boolean
formatStr
A string tool to construct a string with format string.
meta | description |
---|---|
返回 | String |
定义于 | cocos2d/core/platform/js.js:624 |
参数列表
msg
String | Any A JavaScript string containing zero or more substitution strings (%s).subst
Any JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.
示例
cc.js.formatStr("a: %s, b: %s", a, b);
cc.js.formatStr(a, b, c);
createMap
该方法是对 Object.create(null)
的简单封装。Object.create(null)
用于创建无 prototype (也就无继承)的空对象。这样我们在该对象上查找属性时,就不用进行 hasOwnProperty
判断。在需要频繁判断 hasOwnProperty
时,使用这个方法性能会比 {}
更高。
meta | description |
---|---|
返回 | Object |
定义于 | cocos2d/core/platform/js.js:675 |
参数列表
forceDictMode
Boolean 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.