It is important to understand that the word “object” is used here not in the sense of “an object of a certain class”. An object in JavaScript is first of all just a collection of properties (if it is easier for someone, it can be called an associative array or list) consisting of key-value pairs. Moreover, the key can only be a string (even for array elements), while the value can be any data type listed below.
test=function() {alert('Hello!')} // {alert('Hello!')} ( , , ) test
test_link=test; //test_link
test(); //Hello!
test_link(); //Hello!
* This source code was highlighted with Source Code Highlighter.
test={prop: 'sometext'} // prop
test_link=test; //
alert(test.prop); //sometext
alert(test_link.prop); //sometext
//
test_link.prop='newtext';
alert(test.prop); //newtext
alert(test_link.prop); //newtext
/* , - .
- . , , . */
//
test.new_prop='hello';
delete test.prop;
alert(test_link.prop); //undefined -
alert(test_link.new_prop); //hello -
//
delete test;
alert(test.new_prop);
/* , test , test.new_prop */
alert(test_link.new_prop); //hello
/* , , . test_link */
//
test=test_link; // test
test_link={prop: 'sometext'} //
alert(test_link.prop); //sometext
alert(test.prop); //undefined
/* C , test test_link .
, test_link , */
alert(test.new_prop); //hello - test
* This source code was highlighted with Source Code Highlighter.
obj=new String('hello'); //
simple='hello'; //
alert(obj); //hello
alert(simple); //hello -
alert(obj.length); //6 - String length,
alert(simple.length); //6
/* simple - , , String. */
obj.prop='text';
simple.prop='text';
alert(obj.prop); //text - obj ,
alert(simple.prop); //undefined - simple ,
* This source code was highlighted with Source Code Highlighter.
test={
simple_property: 'Hello',
object_property: {
user_1: '',
user_2: ''
},
function_property: function(user) {
alert(this.simple_property + ', ' + this.object_property[user]);
}
}
test.function_property('user_1'); //Hello, .
* This source code was highlighted with Source Code Highlighter.
test.function_property('user_1'); //Hello, .
test2=new Object(); // , test2={}
test.function_property.call(test2, 'user_1'); //
/* call . , function_property test, this test, test2. .. object_property, this.object_property[user] */
//
test2.simple_property='Good day';
test2.object_property=test.object_property; // ,
test.function_property.call(test2, 'user_1'); //Good day, .
* This source code was highlighted with Source Code Highlighter.
make_me=function(_name) {
alert(' ');
this.name=_name;
this.show_name=function() {alert(this.name);}
}
child=new make_me(''); //
/* , . new , . .. make_me - , , make_me, this . name, _name, show_name. ( , ) child , */
alert(child.name); //
child.show_name(); //
child2=new make_me('');
child2.show_name(); //
child2.show_name=function() {alert(' ');} // ,
child2.show_name(); //
child.show_name(); // -
* This source code was highlighted with Source Code Highlighter.
, , Object (Function, Array ) — , , ..
make_me=function(_name) {
alert(' ');
this.name=_name;
this.show_name=function() {alert(this.name);}
}
/*
function, , .. - , . , ( ) prototype, . , make_me.prototype=new Object();
, ( prototype) constructor, . .
, {constructor: ... ...} - .
*/
alert(typeof make_me.prototype); //Object - ,
alert(typeof make_me.prototype.constructor); //Function -
alert(make_me.prototype.constructor === make_me); //true
make_me.prototype.set_name=function(_name) {this.name=_name;} // make_me
child=new make_me(''); //
/* , , child [[Prototype]], , make_me.prototype. .. , , - */
alert(child.name); //
child.show_name(); //
child.set_name('');
/* , set_name child. , child.[[Prototype]], . */
child.show_name(); // - :)
make_me.prototype.show_name2=function() {alert(', ' + this.name;} //.. - ,
child2=new make_me('');
child2.show_name2(); //,
child.show_name2(); //, - ,
child2.show_name2=function() {alert(' ');} // , show_name2 ( ) ""
child2.show_name2(); // - .. show_name2, ,
child.show_name2(); //, -
make_me.prototype={prop: 'hello'} //
alert(child.prop); //undefined
child.show_name2(); //,
/* , , . , [[Prototype]] child child2 ( make_me), make_me.prototype - , make_me */
child3=new make_me('');
alert(child3.prop); //hello -
* This source code was highlighted with Source Code Highlighter.
make_me=function(_name) {
alert(' ');
this.name=_name;
this.show_name=function() {alert(this.name);}
}
make_me.prototype.set_name=function(_name) {this.name=_name;}
child=new make_me('');
alert(typeof make_me.prototype); //object - prototype
alert(typeof child.prototype); //undefined - prototype
alert(child.constructor.prototype === make_me.prototype); //true - constructor, - make_me, , , prototype
* This source code was highlighted with Source Code Highlighter.
bird=function() {} //
bird.prototype.cry=function(){alert('!');} //
bird.prototype.fly=function(){alert(' !');} //
duck=function() {}
duck.prototype=new bird();
duck.prototype.cry=function(){alert('-!');} //
duck.prototype.constructor=duck; // prototype.constructor duck, .. bird
billy = new duck(); // -
billy.fly(); // ! - ,
billy.cry(); //-! - -,
* This source code was highlighted with Source Code Highlighter.
make_me=function() {}
child=new make_me();
alert(child.toString()); // [object]
* This source code was highlighted with Source Code Highlighter.
//
man=function() {
this.live=function(){alert(' ');} //
this.walk=function(){alert(' ');} //
}
//
poet=function(){
this.kill=function(){alert(' ');} //
this.live=function(){alert(' ');} //
}
vladimir=new man(); // -
vladimir.live(); // -
vladimir.walk(); // -
poet.call(vladimir); // poet vladimir
vladimir.kill(); //
vladimir.live(); //
//
man.call(vladimir);
vladimir.live(); //
* This source code was highlighted with Source Code Highlighter.
Source: https://habr.com/ru/post/48542/
All Articles