JavaEye javascript object-oriented technology foundation (1)

Transfer from:

Director: sdcyst Read: 27133 times Comments: 80 Update Time :2008-12-15
Introduction javascript read a lot of object-oriented technology, the article is very faint. Why? Not because of poorly written, but too esoteric.
javascript objects not explain how matter, a straight up on the subject, classes / inheritance / prototype / private variables ....
The result, seen most of the day, with a general understanding of, and makes an aftertaste, as if nothing to understand ...
This article is for reference <<javascript-the definitive guide,5th edition>> Chapter 7,8,9 s and written, and I will try according to the original structure of the book to illustrate the javascript object-oriented technology (Object / Array - "Functions -" Class / constructor / prototype). For some of my Insincere and are not allowed to place myself, I will attach the original English sentences, for your reference.
If we did not show that the text appears all the English sentences (except for program body) are quoted from <<javascript-the definitive guide,5th edition>>.

Objects and arrays (Objects and Arrays)
What is the object? To a number of "name - Property," a combination of a unit on the inside, on the formation of an object. We can be interpreted as javascript objects is that some "key - value" pairs (An object is a collection of named values. These named values are usually referred
to as properties of the object .-- Section3.5).
"Name" can only be a string type, can not be other types, but the type attribute is arbitrary (digital / string / other objects ..). Can use new Object () to create an empty object, you can simply use "()" to create an empty object, which is equivalent to the role of the two.

var emptyObject1 = {};           // Creates an empty object
var emptyObject2 = new Object(); // Creates an empty object
var person = {"name":"sdcyst",
          "sex":"male"};     // Creating a person object that contains the initial value of
alert(;              //sdcyst
alert(person["age"]);            //18

From the above examples we can see that access to an object's properties, you can simply use the object name plus "." Added the name attribute, you can also use "[]" operator to obtain, this time in [] inside the The property name should be in quotes, because the index is a string object type.
javasript object is a variable number of attributes in the creation of an object at any time after any of the attributes assigned to it.

var person = {}; = "sdcyst";
person["age"] = 18;
alert( + "__" + person.age); //sdcyst__18   

var _person = {name:"balala","age":23}; // In constructing an object  , Property names do not have to quote to dimension  (name),
                    // But still is a string type  . When in access  [] Still need quotation marks within the
alert(_person["name"] + "__" + person.age); //balala__23
alert(_person[name]);                   //undefinied

Through "." Operator to obtain the properties of an object, you must have to know the property name. Generally speaking, "[]" operator to obtain the object properties of a number of more powerful,
In the [] to put some expressions to get the property value,
For example can be used in loop control statements, while "." Operators do not have this flexibility.

var name = {"name1":"NAME1","name2":"NAME2","name3":"NAME3","name4":"NAME4"};
var namestring = "";
for(var props in name) {  // Circular name properties in an object name
    namestring += name[props];
alert(namestring);  //NAME1NAME2NAME3NAME4   

namestring = "";
for(var i=0; i<4; i++) {
    namestring += name["name"+(i+1)];
alert(namestring);  //NAME1NAME2NAME3NAME4

delete operator to delete an object in an attribute to determine whether there is an attribute can be used "in" operator.

var name = {"name1":"NAME1","name2":"NAME2","name3":"NAME3","name4":"NAME4"};
var namestring = "";
for(var props in name) {  // Circular name properties in an object name
    namestring += name[props];
alert(namestring);  //NAME1NAME2NAME3NAME4   

delete name.name1;  // Delete name1 property
delete name["name3"];  // Delete name3 property
namestring = "";
for(var props in name) {  // Circular name properties in an object name
    namestring += name[props];
alert(namestring);  //NAME2NAME4   

alert("name1" in name); //false
alert("name4" in name); //true

Note that the properties of the object is not in order.

Each object's constructor property of a javascript object has a constructor property. This attribute corresponds to the object is initialized when the constructor function (the function is object).

var date = new Date();
alert(date.constructor);  //Date
alert(date.constructor == "Date");  //false
alert(date.constructor == Date);  //true
分类:Web 时间:2009-06-30 人气:244
blog comments powered by Disqus


  • Java String type parameters in the problem resolution 2010-08-31

    First, the introduction of the sample public class StringAsParamOfMethodDemo { public static void main(String[] args) { StringAsParamOfMethodDemo sapm = new StringAsParamOfMethodDemo(); sapm.testA(); } private void testA() { String originalStr = "ori

  • js利用prototype调用Array的slice方法示例 2014-06-27

    这篇文章主要介绍了如何利用js的prototype调用Array的slice方法,需要的朋友可以参考下 <script type="text/javascript"> function fn(name){ if(typeof name === "string"){ var args = arguments, 1 ); for(var i=0;i<args.length;i++){ alert(

  • Further understanding of javascript objects. Array and hash table [reproduced] 2010-04-05

    In javascript, the object is actually a hash table, such as following the user object: function user (n, a) ( = n; this.age = a; this.toString = function () ( return "Name:" + + ", Age:" + this.age; ) ) var u = new

  • The prototype property of JavaScript objects 2010-04-28

    This article written more clearly The prototype property of JavaScript objects, can return a reference to the prototype object type. It is quite a mouthful to explain, to understand it, must first correctly understand the object type (Type) and proto

  • [ExtJS3.2 source one hour a day] ExtJS in the String type of template format (11) 2010-09-21

    ExtJS on the String type of extended format method: //format Specified format format : function(format){ // Remove the argument from 1 Start interception, is to get rid of format This parameter var args = Ext.toArray(arguments, 1); // Replace the pla

  • ruby string type with grammar 2009-03-19

    String Class methods : String type of commonly used methods: text_helper.rb Capitalize the first letter : # a = "hello" # a.capitalize! #=> "Hello" # a #=> "Hello" # a.capitalize! #=> nil Conversion comparison : # &q

  • String type object is changed 2010-04-16

    Because String is designed to not change (immutable) class, so it's all the objects are immutable objects. See the following code: String s = "Hello"; s = s + "world!"; s the point of the object is changed? From the first conclusion of

  • Oracle of Clob and String type conversion 2010-06-12

    At the Forum to see a problem, I've never used Clob, because they were not this demand, but in order to grab points, how to find the fastest solution for it, the first thought of spring, because the spring on the orm tool package like ibatis, hiberna

  • oracle string type Xiangjie 2010-07-03

    Sort out the char, character, varchar, varchar2, nchar, nvarchar2, long, long raw, and LOB types of comparison between First, the concept type char: Fixed-length string type, when the stored data is not reached the set maximum, it will append a space

  • informix string type Xiangjie (including comparison with the oracle) 2010-07-03

    lvarchar after the 9.4 version is available with a specified length of ways, such as lvarchar (3000), not with the length of the default is 2048 bytes, maximum length is 32,739 bytes. So for the previous version 9.4, can not be used with a length of

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.142 (s). 12 q(s)