MK.Object
, which is inherited from the Matreshka
class. The idea is simple: we have a lot of keys responsible for the data in the class instance and we believe that the other properties are responsible only for the state of the application and are not a business model.._keys
property ._keys
is responsible for the set of keys, which is an object with values ​​that we do not care about. The array would not suit us, because, before adding a new key, we would have to check if there is a key in the array, and when deleting, we would have to recognize the index, then shift the following elements. In the case of an object, we get a full set of rows; to add a new key, it is not necessary to check its presence, and to delete it, you just need to call the delete
operator..jset
: var mkObject = new MK.Object(); mkObject.jset( 'a', 1 ); console.log( mkObject.toJSON() ); // { a: 1 }
var mkObject = new MK.Object(); mkObject.jset( 'a', 1 ); mkObject.a = 2; console.log( mkObject.toJSON() ); // { a: 2 }
Matreshka
class: mkObject.bindNode( 'a', '.my-element' ); mkObject.on( 'change:a', handler );
.toJSON
method .toJSON
we will not see this property: var mkObject = new MK.Object(); mkObject.jset( 'a', 1 ); mkObject.b = 3; console.log( mkObject.toJSON() ); // { a: 1 }
.addJSONKeys
method mkObject.addJSONKeys( 'b', 'c' );
.toJSON
function, always use .jset
instead of the usual assignment. If the keys are known, then I personally prefer to always set the default data: var MyClass = Class({ 'extends': MK.Object, constructor: function( data ) { this .initMK() .jset({ // a: 1, b: 2, c: 3 }) .set( data ) // (data - ) ; } });
.initMK
call. Here he initializes not only event objects and “special” properties, but also an object — a set of keys. In addition, it adds the necessary event handlers to trigger the "modify"
event when the data changes. Example: var mkObject = new MK.Object(); mkObject.jset({ a: 1, b: 2 }); mkObject.c = 3; mkObject.on( 'modify', function() { alert( 'Data is changed' ); }); mkObject.a = 4; // mkObject.b = 5; // mkObject.c = 6; // , "c" ,
var mkObject = new MK.Object({ a: 1, b: 2 }); // , var mkObject = new MK.Object(); mkObject.jset({ a: 1, b: 2 });
.each
method: var mkObject = new MK.Object(); mkObject.jset({ a: 1, b: 2 }); mkObject.c = 3; mkObject.each( function( item, key ) { console.log( key ); }); // 'a', 'b'
.hasOwnProperty
var mkObject = new MK.Object(); mkObject.jset( 'a', 1 ); mkObject.b = 2; alert( mkObject.hasOwnProperty( 'a' ) ); // true alert( mkObject.hasOwnProperty( 'b' ) ); // false ('b' )
for..in
construction, as for a regular object: for( var i in mk ) if( mk.hasOwnProperty( i ) ) { doSomething(i, mk[i]) }
Object.defineProperty
.keyOf
, which searches for a key by value and returns a key (analog of .indexOf
for an array)..keys
, which returns an array of keys..removeJSONKeys
, which removes keys from the set of keys responsible for data. ... constructor: function () { this .initMK() .jset({ // userName: '', password: '', rememberMe: true }) .bindings() .events(); }, ...
.toJSON
method: ... login: function () { if (this.isValid) { alert( JSON.stringify( this.toJSON() ) ); } return this; } ...
MK.Array
class, which I will discuss in the next article.Source: https://habr.com/ru/post/196886/
All Articles