***** JavaScript object-oriented object-wide solution [half of original] *****

Author: Topcss QQ: 419074376 E-mail: andpai1.0 @ gmail.com Time :2008-11 on

Background: JavaScript quick contacts for two years. From the start of her full of curiosity, and now a lot of study about Her framework. Friend ask me: there are so many frameworks, why should you go study the bottom of this object-oriented? In fact, this question: Another Friend has been answered for me. No matter how future changes in the framework, as long as the understanding of the underlying principle is very easy to learn up-to-use. Well, we begin the study.

1, create the object:

var obj = new Object ();// first approach.

var obj = {};// second method. Passing through a var arr = [] to create an array

2, give the object property assignment

nahao.gender = 'male';

nahao.yearOfBirth = 1989;

nahao.name = 'Topcss'; / / object property can also be called a member of the object. The same as an ordinary variable, JS objects are the property could be strings, arrays, numbers, or even object or function.

3, give the object add object property in fact function if it is, then this property can be called object-Ways

nahao.gender = 'male';

nahao.yearOfBirth = 1989;

nahao.name = 'Topcss';

nahao.info = function () (

var str = 'Name:' +

this.name + ', Sex:' +

this.gender + ', born in the year:' +

this.yearOfBirth;

alert (str);

)

nahao.info ();

4, we can use the following definition of grammar briefly above object:

var nahao = (

gender: 'male',

yearOfBirth: 1989,

name: 'Topcss',

info: function () (

var str = 'Name:' + this.name + ', gender:' + this.gender + ', born in the year:' + this.yearOfBirth;

alert (str);

)

); / / If the call nahao.info (), will be and the same results above. Note that the use of property between the comma-separated, and finally there is no comma after a property.

5, constructor constructor can help us to reduce the code volume. First of all, the constructor is a function. Prototype is as follows:

function Person (){}/ and the definition of an ordinary function is no different. Person on the following constructor to add content:

function Person (name, gender, yearOfBirth, site) (

this.name = name;

this.gender = gender;

this.yearOfBirth = yearOfBirth;

this.site = site;

this.info = function () (

var str = 'Name:' + this.name + ', gender:' + this.gender

+ ', Birth year:' + this.yearOfBirth + 'website:' + this.site;

alert (str);

)

) / / This constructor is complete. We now can use the following statement to define a number of objects:

var nahao = new Person ( 'Top css', male, 1989,' www.javava.org);

var gaoshou = new Person ( 'Widen', 'male', 1980, 'www.baidu.net');

var wudi = new Person ( 'unknown', 'do not know', 'confidential', 'bbs.blueidea.net'); through their approach to the call

nahao.info ();

gaoshou.info ();

wudi.info ();

Note: In the above constructor, the parameter name and the object property names are the same, such as:

this.name = name; While this is legitimate, but if the definition of private property, then on the point of death (behind will be mentioned), but looks a bit confusing. So it would be preferable to amend the constructor as follows:

function Person (nameArg, genderArg, yearOfBirthArg, siteArg) (

this.name = nameArg;

this.gender = genderArg;

this.yearOfBirth = yearOfBirthArg;

this.site = siteArg;

this.info = function () (

var str = 'Name:' + this.name + ', gender:' + this.gender

+ ', Birth year:' + this.yearOfBirth + 'website:' + this.site;

alert (str);

)

)

6, What is a static method of static methods? Is added directly at an object on the property or method, which only an object of effective, such as:

nahao.skill = 'will point XHTML, CSS, now also say that the JavaScript'; Add END after the statement, nahao have a skill on the property. But from the same constructor gaoshou and is not subject to any influence wudi. Of course, we can also give them to add this property:

gaoshou.skill = 'proficiency in HTML, CSS, JS, FLEX, PHP,. NET, Linux Programming, Perl, Ruby, XXX ...';

wudi.skill = 'use of beer bottle cap with hand-polished sand out the basic PC components such as CPU. ';

Similarly, we can also give constructor to add a static method, but it will not affect the same use of the definition of the object constructor. For example:

Person.showName = function () (

alert (this.name);

);

Person.showName ();

nahao.showName ();// we give the definition of a Person ShowName function, but only on the constructor Person itself useful (but no real purpose), if at nahao object calling this method will make mistakes.

7, give the definition of the private members of the target object open? In front of the definition of all objects, all of their property are exposed and can easily access, modify or even fine. For example we can nahao.name direct access to the object's name property and so on. If we want to have a confidential foreign property, you can give the definition of the object of private property:

function Person (nameArg, genderArg, yearOfBirthArg, siteArg, privacyArg) (

... ...

var privacy = privacyArg;

... ...

)

var nahao = new Person ( 'Top css', male, 1989,' www.javava.org ',' there is 10 with the toes');

alert (nahao.privacy); / / to the constructor must make changes, we give the object nahao added a privacy property, if trying to alert its value will be displayed if undefined.

Come take a look at the following private method, if we are on the Person constructor with the following modifications:

function Person (nameArg, genderArg, yearOfBirthArg, siteArg, privacyArg) (

function insideOnly () (

alert ( 'I only can call the internal object!');

)

) / / Then insideOnly will not be able to be outside of the visit, if you try to perform this function will be wrong. Note that private methods and did not bind to the target, it's this keyword does not point to objects. If you need to invoke private methods within the object, then you can use the following methods:

function Person (nameArg, genderArg, yearOfBirthArg, siteArg, privacyArg) (

... ...

var myOwner = this;

function insideOnly () (

alert (myOwner.name);

)

) / / First constructor in the definition of a myOwner variables, this keyword will be assigned to it, and then you can use at private functions of this variable to access the object itself.

8, privileges Ways at private members should not object to external access, the object of the visit should not have what is the use? Ways we can use the privilege to visit these private property. So-called privileges to the use of this keyword is the definition of the function, such as:

function Person (nameArg, genderArg, yearOfBirthArg, siteArg, privacyArg) (

... ...

this.showPrivacy = function () (

var str = 'secret:' + this.name + privacy + '!';

alert (str);

);

)

var nahao = new Person ( 'Top css', male, 1989,' www.javava.org ',' there is 10 with the toes');

nahao.showPrivacy ();

We first give Person constructor to add a privilege showPrivacy methods, methods used in the value of private variable privacy. Finally we call on nahao object of the method, results are as follows:

9, prototype Analysis First, we want to reject this sentence above "on the same Person.prototype changes will affect the use of the definition of the object of Person." See the following code:

Person.prototype = (

mark: 'Person.Proto'

)

var nahao = new Person ( 'Top css', male, 1989,' www.javava.org ');

Person.prototype = (

mark: 'Person.NewProto'

)

alert (nahao.mark);

The output will alert what is it? According to the above experience in the alert before the final changes Person.prototype its mark for the property assignment 'Person.NewProto'. But the actual output: Person.Proto

This is not a bug, but all browsers are the same. Reasons are as follows:

Person.prototype = (

mark: 'Person.Proto'

)

Section of the first volume to create an object literal and give assignment Person.prototype, we call this object literal of Proto.

var nahao = new Person ( 'Top css', male, 1989,' www.javava.org ');

In the creation of objects and in accordance with nahao constructor to initialize the object at the same time, will quietly give nahao target set an internal property, we put all named xxx, and its assignment to Person.prototype value, that is, the above-mentioned the volume of the object literal Proto, JavaScript dynamic inheritance is achieved by the xxx. (This JavaScript implementation of the internal mechanism is not my forehead want to beat out Netscape staff are in an article mentioned, the following procedures will be used to prove that this flow.) And,

Person.prototype = (

mark: 'Person.NewProto'

)

Here we will create another volume of an object literal NewProto, and assignment to Person.prototype. Here is quite clear, although we have amended the Person.prototype, but did not affect the object nahao. When we visited nahao.mark, it first internal look at themselves, unable to find will find xxx, naturally found a Proto, rather than NewProto. Well, the implementation of dynamic inheritance xxx key what is it? In FireFox it is __proto__, have the following code for the card. As for the IE in me xxx what is not found.

分类:AJAX 时间:2009-04-04 人气:567
分享到:
blog comments powered by Disqus

相关文章

  • Js through the array to achieve the 3 methods 2010-08-31

    When our page check box in the same group, we need to use JS to traverse the value, if the check amount to many thousands, we need to consider the efficiency of the problem, after all, is running in the JS client, the requirements of the client machi

  • 实例讲解JS中数组Array的操作方法 2014-04-04

    这篇文章主要介绍了JS中数组Array的操作方法,并用代码实例讲解,需要的朋友可以参考下 js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^ var arr = new Array(); arr[0] = "aaa"; arr[1] = "bbb"; arr[2] = "ccc"; //alert(arr.length);//3 arr.pop(); //alert(arr.length);//2 //a

  • JS(JQuery)操作Array的相关方法介绍 2014-08-01

    本篇文章主要是对JS(JQuery)操作Array的相关方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 1:split 函数将字符串按某个字符分割,将分割后的结果存入字符串数组中 function SplitUsersInformation(users) { var usersArray = users.split(';'); return usersArray; } 2:substr 函数切割目标字符串 复制代码 代码如下: currentStr = currentStr

  • JS中使用Array函数shift和pop创建可忽略参数的例子 2014-08-15

    这篇文章主要介绍了JS中使用Array函数shift和pop创建可忽略参数的例子,这是一种比较高级的应用,需要的朋友可以参考下 在 JS Array 中支持两个方法,shift() 和 pop(),分别是指从一个数据中的最前面和最后面删除一个值,并返删除值.看一个示例就明白了: var arr = ['s','o','f','i','s','h']; arr.shift(); // 返回 's' arr; // 目前是 ['o','f','i','s','h'] arr.pop() // 返回

  • js中数组(Array)的排序(sort)注意事项说明 2014-08-27

    本篇文章主要是对js中数组(Array)的排序(sort)注意事项进行了说明介绍,需要的朋友可以过来参考下,希望对大家有所帮助 直接看代码吧,测试结果也贴在里面了 var arrDemo = new Array(); arrDemo[0] = 10; arrDemo[1] = 50; arrDemo[2] = 51; arrDemo[3] = 100; arrDemo.sort(); //调用sort方法后,数组本身会被改变,即影响原数组 alert(arrDemo);//10,100,50,5

  • JS中数组Array的用法示例介绍 2015-04-05

    这篇文章主要介绍了JS中数组Array的用法,需要的朋友可以参考下 new Array() new Array(len) new Array([item0,[item1,[item2,...]]] 使用数组对象的方法: var objArray=new Array(); objArray.concact([item1[,item2[,....]]]-------------------将参数列表连接到objArray的后面形成一个新的数组并返回,原有数组不受影响.如:var arr=["a&qu

  • 冇内容管理系统分析-js中关于array的slice和sort方法(转自JavaEye) 2010-03-30

    阅读: 17 评论: 0 作者: 阿K&LiveCai 发表于 2010-01-02 23:34 原文链接 js中关于array的slice和sort方法(转自JavaEye) 一.array.slice(start, end) 方法: slice()是用来截取数组中的一部分,用它来复制数组,如果省略了end参数,则切分的数组包含从start开始到数组结束的所有元素. 现在要用它来复制数组,就一行: var newArray=oldArray.slice(0); 其他说明: 1. 如果

  • Daquan js, javascript code, js code integration 2010-12-22

    Event source object event.srcElement.tagName event.srcElement.type Capture Release event.srcElement.setCapture(); event.srcElement.releaseCapture(); Events button event.keyCode event.shiftKey event.altKey event.ctrlKey Event return values event.retur

  • Page Kanban Musume js Edition (Forum Mother. Blog tan. In short a dream tan V (-1) code 2010-04-12

    /** * ukagaka.js * @author Star */ if(typeof(Kangbang) == 'undefined') var Kangbang = {}; var Ukagaka = null; Kangbang.ukagaka = function(){ this.msgList = []; // The available message list this.shellList = [];// The available list of shell this.msgI

  • js对象之JS入门之Array对象操作小结 2015-05-01

    每天一对象,今天我们也来new一个.没有系统的学过JS,没有特别的写过一个比较出色的类库,没有运用过一个很强的类库,prototype.js在进行着,慢慢的前进相信不久的将来就可以应用prototype.js来开发自己的应用程序了. 不过在学脚本prototype.js的过程中发现有些方法我们很少用到,但是有些方法好像很经典,脚本库在不断的加大,眼前一片...继续我们JS入门,今天学习一下Array数组. 首先我们来看看它的定义: var arrayObj = new Array() var a

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.319 (s). 10 q(s)