JavaEye javascript object-oriented technology foundation (1)

Transfer from: http://www.javaeye.com/wiki/Object_Oriented_JavaScript/1279-javascript-object-oriented-technology-one

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",
          "age":18,
          "sex":"male"};     // Creating a person object that contains the initial value of
alert(person.name);              //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 = {};
person.name = "sdcyst";
person["age"] = 18;
alert(person.name + "__" + 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 人气:228
分享到:
blog comments powered by Disqus

相关文章

  • Java-String type parameter problem (change) 2010-08-11

    Original Source http://freej.blog.51cto.com/235241/168676 Just saw a brother in the Java's String-value / pass by reference puzzled rummaging through to find a long time ago I wrote this article, send in here, hoping to confuse some friends to help. A

  • 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 = Array.prototype.slice.call( arguments, 1 ); for(var i=0;i<args.length;i++){ alert(args[i]);//结果:

  • [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 placehol

  • 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 : # "abcdef".casecmp("abcde") #=>

  • Object-oriented programming. Array. Run time data type checking 2010-04-11

    Flex and java object-oriented like, here only some attention to points In the flex in only one constructor Modifiers: private: Private internal: the same package, the sub-class can not access public: public protected: sub-class can access Constants: ...

  • 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 conc ...

  • 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, hibernat

  • 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 (see

  • 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 expre

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

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