mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 18:40:08 +00:00
several minor tweaks...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
a304b7d91f
commit
d9526e4c76
@ -619,6 +619,13 @@ create(<base>)
|
|||||||
-> <obj>
|
-> <obj>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For functions we can set `.name`
|
||||||
|
```bnf
|
||||||
|
create(<name>, <base-func>)
|
||||||
|
-> <func>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
This is similar to [`Object.create(..)`] but handles callables correctly, i.e. if
|
This is similar to [`Object.create(..)`] but handles callables correctly, i.e. if
|
||||||
`<base>` is a callable then `<obj>` will also be callable.
|
`<base>` is a callable then `<obj>` will also be callable.
|
||||||
|
|
||||||
|
|||||||
28
object.js
28
object.js
@ -273,9 +273,12 @@ var create =
|
|||||||
module.create =
|
module.create =
|
||||||
function(obj){
|
function(obj){
|
||||||
// name given...
|
// name given...
|
||||||
|
var name = ''
|
||||||
if(typeof(obj) == 'string' && arguments.length > 1){
|
if(typeof(obj) == 'string' && arguments.length > 1){
|
||||||
var name
|
;[name, obj] = arguments
|
||||||
;[name, obj] = arguments }
|
// sanity check...
|
||||||
|
if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)){
|
||||||
|
throw new Error(`create(..): invalid name: "${name}"`) } }
|
||||||
// calable...
|
// calable...
|
||||||
if(typeof(obj) == 'function'){
|
if(typeof(obj) == 'function'){
|
||||||
var func = function(){
|
var func = function(){
|
||||||
@ -286,10 +289,12 @@ function(obj){
|
|||||||
// NOTE: if obj does not inherit from Function .call
|
// NOTE: if obj does not inherit from Function .call
|
||||||
// might not be available...
|
// might not be available...
|
||||||
: Function.prototype.call.call(obj, func, ...arguments) }
|
: Function.prototype.call.call(obj, func, ...arguments) }
|
||||||
|
func.name = name
|
||||||
// XXX set the name....
|
func.name != name
|
||||||
// XXX
|
&& (func = eval('('+
|
||||||
|
func
|
||||||
|
.toString()
|
||||||
|
.replace(/function\(/, `function ${name}(`) +')'))
|
||||||
func.__proto__ = obj
|
func.__proto__ = obj
|
||||||
// XXX not sure about this yet...
|
// XXX not sure about this yet...
|
||||||
Object.defineProperty(func, 'toString', {
|
Object.defineProperty(func, 'toString', {
|
||||||
@ -937,6 +942,10 @@ module.C =
|
|||||||
function Constructor(name, a, b, c){
|
function Constructor(name, a, b, c){
|
||||||
var args = [...arguments].slice(1, 4)
|
var args = [...arguments].slice(1, 4)
|
||||||
|
|
||||||
|
// sanity check...
|
||||||
|
if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)){
|
||||||
|
throw new Error(`Constructor(..): invalid name: "${name}"`) }
|
||||||
|
|
||||||
// parse args...
|
// parse args...
|
||||||
// Constructor(name[[, constructor[, mixin]], proto])
|
// Constructor(name[[, constructor[, mixin]], proto])
|
||||||
var proto = args.pop() || {}
|
var proto = args.pop() || {}
|
||||||
@ -1007,10 +1016,11 @@ function Constructor(name, a, b, c){
|
|||||||
// NOTE: this eval(..) should not be a risk as its inputs are
|
// NOTE: this eval(..) should not be a risk as its inputs are
|
||||||
// static and never infuenced by external inputs...
|
// static and never infuenced by external inputs...
|
||||||
// NOTE: this will fail with non-identifier names...
|
// NOTE: this will fail with non-identifier names...
|
||||||
_constructor.name == 'Constructor'
|
_constructor.name != name
|
||||||
&& eval('_constructor = '+ _constructor
|
&& (_constructor = eval('('+
|
||||||
|
_constructor
|
||||||
.toString()
|
.toString()
|
||||||
.replace(/Constructor/g, name))
|
.replace(/Constructor/g, name) +')'))
|
||||||
// set .toString(..)...
|
// set .toString(..)...
|
||||||
// NOTE: this test is here to enable mixinFlat(..) to overwrite
|
// NOTE: this test is here to enable mixinFlat(..) to overwrite
|
||||||
// .toString(..) below...
|
// .toString(..) below...
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-object",
|
"name": "ig-object",
|
||||||
"version": "5.5.1",
|
"version": "5.5.2",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "object.js",
|
"main": "object.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user