js.private
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -21,10 +21,24 @@ "use strict"; | ||
this.props = props; | ||
this.key = "_" + Math.random().toString(36).substr(2, 7) + "_"; | ||
this._key_ = "_" + Math.random().toString(36).substr(2, 7) + "_"; | ||
this._props_ = this.prepare(props); | ||
} | ||
_createClass(Private, [{ | ||
key: "prepare", | ||
value: function prepare(props) { | ||
for (var attr in props) { | ||
if (props.hasOwnProperty(attr) && typeof props[attr] === "function") props[attr] = this.bind(this._key_, props[attr]); | ||
}return props; | ||
} | ||
}, { | ||
key: "bind", | ||
value: function bind(key, method) { | ||
return function () { | ||
return method.apply(this[key], arguments); | ||
}; | ||
} | ||
}, { | ||
key: "get", | ||
value: function get(context) { | ||
return context[this.key] || this.set(context); | ||
return context[this._key_] || this.set(context); | ||
} | ||
@@ -34,14 +48,8 @@ }, { | ||
value: function set(context) { | ||
var clone = (0, _jsClone2["default"])(this.props); | ||
for (var attr in clone) { | ||
if (clone.hasOwnProperty(attr) && clone[attr] instanceof Function) clone[attr] = this.bind(clone[attr], context); | ||
}return context[this.key] = clone; | ||
var props = Object.create(this._props_); | ||
for (var attr in this._props_) { | ||
if (this._props_.hasOwnProperty(attr) && typeof this._props_[attr] === "object") props[attr] = (0, _jsClone2["default"])(this._props_[attr]); | ||
}props[this._key_] = context; | ||
return context[this._key_] = props; | ||
} | ||
}, { | ||
key: "bind", | ||
value: function bind(method, context) { | ||
return function () { | ||
return method.apply(context, arguments); | ||
}; | ||
} | ||
}]); | ||
@@ -48,0 +56,0 @@ |
{ | ||
"name": "js.private", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Private properties for objects", | ||
@@ -5,0 +5,0 @@ "main": "./dst/Private.js", |
@@ -7,24 +7,32 @@ import Clone from "js.clone"; | ||
constructor( props ){ | ||
this.props = props; | ||
this.key = `_${ Math.random().toString( 36 ).substr( 2, 7 ) }_`; | ||
this._key_ = `_${ Math.random().toString( 36 ).substr( 2, 7 ) }_`; | ||
this._props_ = this.prepare( props ); | ||
} | ||
prepare( props ){ | ||
for( let attr in props ) | ||
if( props.hasOwnProperty( attr ) && typeof props[ attr ] === `function` ) | ||
props[ attr ] = this.bind( this._key_, props[ attr ] ); | ||
return props; | ||
} | ||
bind( key, method ){ | ||
return function(){ | ||
return method.apply( this[ key ], arguments ); | ||
}; | ||
} | ||
get( context ){ | ||
return context[ this.key ] || this.set( context ); | ||
return context[ this._key_ ] || this.set( context ); | ||
} | ||
set( context ){ | ||
let clone = Clone( this.props ); | ||
for( let attr in clone ) | ||
if( clone.hasOwnProperty( attr ) && clone[ attr ] instanceof Function ) | ||
clone[ attr ] = this.bind( clone[ attr ], context ); | ||
return context[ this.key ] = clone; | ||
let props = Object.create( this._props_ ) | ||
for( let attr in this._props_ ) | ||
if( this._props_.hasOwnProperty( attr ) && typeof this._props_[ attr ] === `object` ) | ||
props[ attr ] = Clone( this._props_[ attr ] ); | ||
props[ this._key_ ] = context; | ||
return context[ this._key_ ] = props; | ||
} | ||
bind( method, context ){ | ||
return function(){ | ||
return method.apply( context, arguments ); | ||
}; | ||
} | ||
} | ||
@@ -38,2 +46,2 @@ | ||
}; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4911
94