***** 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-05-12

    这篇文章主要整理了一下JS数组(Array)处理函数,常用的都汇总了一下,推荐给大家. 1.concat() 连接两个或更多的数组 该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 例如: <script type="text/javascript"> var arr = [1, 2, 3]; var arr1 = [11, 22, 33]; document.write(arr.concat(4, 5, arr1)); </script> 输出结果

  • 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() // 返回

  • 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

  • js数组Array sort方法使用深入分析 2013-10-20

    js中Array.sort()方法是用来对数组项进行排序的,默认是升序排列sort() 方法可以接受一个 方法为参数,这个方法有两个参数,接下来本例将对sort方法进行深入探讨,感兴趣的朋友可以参考下 javascript 中 Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列,实例代码如下: var arrA = [6,2,4,3,5,1]; arrA.sort(); document.writeln(arrA); //结果是:1,2,3,4,5,6 sort()

  • js模拟实现Array的sort方法 2013-10-28

    JavaScript的Array对象有一个sort方法,用于实现对数组元素的排序,该方法默认按照数组项ASCII 字符顺序升序排列. 如[6,7,9,1,-1].sort();执行后数组变为[-1,1,6,7,9]. 对于需要降序排列或非字符串排序,该方法就不能很好的执行了. 当然,sort有一个可选参数,它能帮我们解决这个问题,通过为sort传入一个函数,sort根据函数返回值进行排序. 数组为该函数默认传入数组中的两个元素,如传入a,b,如果函数返回值大于0,则排序方式为b,a,如果返回值小

  • zczhang初学js中的Array对象 2014-07-06

    Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, element1, -, elementn); 参数 参数 size 是期望的数组元素个数.返回的数组,length 字段将被设为 size 的值. 参数 element -, elementn 是参数列表. 返回值 返回新创建并被初始化了的数组. 如果调用构造函数 Array() 时没有使用参数,那

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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