亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

Objects as associative arrays

2019-11-17 04:48:07
字體:
來源:轉載
供稿:網友

  URL: http://www.quirksmode.org/js/associative.Html

On this page I eXPlain how javascript objects are also associative arrays (hashes). Using these you can associate a key string with a value string, which can be very useful sometimes.Suppose you have a combined mouSEOver / click script. You want to keep track of the status of each image, whether it is normal, mouseovered or clicked. In addition you want to be able to reach this status by image name. So if you have an image named 'Home' you want to read outtheStatus['Home']
and get one of the values 'normal', 'mouseover' or 'clicked', corresponding to the current status of the image.
To do this you need Javascript objects.

Objects in JavaScript

JavaScript is an object oriented language. However, in PRactice objects defined by the programmer himself are rarely used, except in complex DOM API's. Of course sUCh standard objects as window and document and their numerous offspring are very important, but they are defined by the browser, not by the programmer.I myself have written JavaScript for more than three years without ever defining an object. The technique explained on this page is the first practical use of programmer-defined objects I've found.Since the only other programming languages I know are Commodore 64 Basic (which is not object oriented, to put it mildly) and Perl (which doesn't need to be object oriented) and since I don't have any formal training in programming I cannot write a general introduction to objects and object oriented programming. Therefore a quick overview will have to suffice.

Methods and properties

In JavaScript you can define your own objects. In addition, you can assign methods and properties to each object, pre-written or self-defined.
  • Methods are 'things that do something', they can be recognized by their brackets (). When you call them, like object.method(), something happens.
  • Properties are 'things that are something'. They have a value, for instance a number, a string or a Boolean value. When you call them, like object.property, you get (or set) this value.
Normal JavaScript functions are also methods (hence the brackets). If you dodocument.write('text')

you execute the pre-defined write() method of the document object. If you write your own functions you add methods to the window object, the parent of all other JavaScript objects.Likewise, if you ask for the innerHeight of a page, you access a property of the window object and if you define a variable of your own you really add a new property to the window object.So you already use methods and properties in everyday JavaScripting. Since most of these are preprogrammed functions and variables, you usually don't need to worry about the objects themselves, they're just a kind of 'black boxes' that contain useful stuff. The methods and properties (functions and variables) that you define yourself are usually added to the window object.

Defining an object and properties

But now we want to create an object of our own. This is simple:var theStatus = new Object;
Now we have initialized our theStatus object and we can start adding properties (in this example we don't need methods). What we want is to create one property for each image on the page. We could dotheStatus.Home = 'normal';
Now we have added a new property Home to our object and set its value to the string 'normal'. (Remember that JavaScript is case sensitive, so the property home does not exist, only Home.)All this is very useful, but using this notation we encounter problems later on. Suppose we want to create a property of theStatus for each image on the page. The property should have the same name as the image and its value should be 'normal'.We cannot do:var x = document.images;
for (var i=0;i<x.length;i++)
{
var theName = x[i].name;
theStatus.theName = 'normal';
}
We go through the entire images array of the page, take the name of each image and then try to create a new property with the same name. But the code above doesn't work. Each time you do theStatus.theName = 'normal';
JavaScript faithfully creates a new property named theName and sets its value to 'normal'. After executing this script you have only one property theName. This is not what we want, we want one property for each image.

Associative arrays

So we have to use one of JavaScript's minor mysteries. In JavaScript, objects are also associative arrays (or hashes). That is, the propertytheStatus.Home
can also be read or written by callingtheStatus['Home']
Thus, you can access each property by entering the name of the property as a string into this array. Such an array associates each key with a value (in this case the key Home is associated with the value normal). In the Perl programming language it is also called a hash.
Unlike Perl, which requires you to create such an associative array explicitly, JavaScript automatically creates a associative array for each object.You see this behaviour with common objects like a form. You can access a form by performing either of these DOM calls:document.forms['theForm']
document.forms.theForm
(You can also use document.theForm but that's a special case, not regular behaviour of JavaScript objects/associative arrays).So when we want to set the status of each image to 'normal' in our object, we dovar x = document.images;
for (var i=0;i<x.length;i++)
{
var theName = x[i].name;
theStatus[theName] = 'normal';
}
and it works. Now theName (a string) is put into the brackets [] where a string is expected. So you create a new key/value pair, which is the same as a new property with a value.All this is JavaScript magic at its fullest. I don't completely understand what I'm doing either, but it works just fine. Basically you now have the power to let one name or string refer to another one.

for (var i in object)

for (var i in object) is equivalent to Perl foreach $key (keys %hash).Just as you can go through each element of a normal array byvar x = [the array];
for (var i = 0;i<x.length;i++)
{
do something with x[i]
}
you can also go through each element of an associative array. Suppose you want to go through the status values of all images. If the status of the image is 'mouseover' you want to call a function callFn() and pass the image name to it. You can of course tediously write out everything:if (theStatus.Home == 'mouseover')
callFn('Home'):
if (theStatus.Place == 'mouseover')
callFn('Place'):
etc.
orif (theStatus['Home'] == 'mouseover')
callFn('Home'):
if (theStatus['Place'] == 'mouseover')
callFn('Place'):
etc.
But this quickly leads to immense scripts. Besides, if you rename an image later on you also have to change a line of code and of course you forget, so you get errors etc.Fortunately JavaScript has the for/in statement which is meant exactly for this situation. If you dofor (var i in theStatus)
{
if (theStatus[i] == 'mouseover')
callFn(i)
}
you go through all properties of the theStatus object (= all keys in the associative array theStatus). The variable i succesively becomes the name of each property of the object (key of the associative array) so you can do something with theStatus[i] and it is done to each property.
In this case, if an image status has the value 'mouseover' you call callFn() and pass it the key (the name of the image).(Note that JavaScript doesn't guarantee any particular order for the properties. So you cannot expect the property that was defined first to appear first, it might come last.)

Test script


A tiny script for your testing pleasure. If you click this link this script is executed:var theStatus = new Object();

function testIt()
{
theStatus.Home = 'mouseover';
theStatus['Place'] = 'click';
for (var i in theStatus)
{
alert('theStatus[/''+i+'/'] is ' + theStatus[i])
}
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩精品电影网| 国内成人精品一区| 亚洲国产精品va在线看黑人动漫| 精品欧美国产一区二区三区| 91久久久久久久久久久| 精品人伦一区二区三区蜜桃免费| 国产精品视频播放| 色偷偷av一区二区三区乱| 97超级碰在线看视频免费在线看| 久久国产一区二区三区| 日韩av在线天堂网| 国产999精品视频| 国产精品久久电影观看| 91国产美女视频| 亚洲午夜性刺激影院| 亚洲r级在线观看| 亚洲成人亚洲激情| 国产亚洲欧美日韩一区二区| 欧美xxxx18性欧美| 91精品在线看| 97avcom| 久久好看免费视频| 97在线观看免费高清| 91亚洲国产成人久久精品网站| 日本三级久久久| 午夜剧场成人观在线视频免费观看| 亚洲欧美日本精品| 亚洲欧美日韩区| 欧美另类在线播放| 国产做受69高潮| 日产精品久久久一区二区福利| 亚洲国产精品久久久| 亚洲经典中文字幕| 久久精品2019中文字幕| 91亚洲永久免费精品| 欧美一区深夜视频| 九九热精品视频| 成人av.网址在线网站| 欧美日韩亚洲视频一区| 成人激情黄色网| 高清一区二区三区日本久| 2018国产精品视频| 欧美成人在线网站| 亚洲日本成人女熟在线观看| 久久久国产精品免费| 亚洲社区在线观看| 国内外成人免费激情在线视频网站| 97香蕉超级碰碰久久免费的优势| 最新亚洲国产精品| 久久91亚洲精品中文字幕奶水| 国产精品欧美激情| 日韩av影视综合网| 欧美裸身视频免费观看| 欧美日韩福利电影| 国产精品久久久久高潮| 日韩欧美亚洲国产一区| www高清在线视频日韩欧美| 亚洲成人av片在线观看| 国产va免费精品高清在线| 亚洲国内精品视频| 欧美最顶级丰满的aⅴ艳星| 国产精品亚洲第一区| 成人激情电影一区二区| 欧美性做爰毛片| 欧美另类在线播放| 久青草国产97香蕉在线视频| 欧美人成在线视频| 欧美成人一二三| 久久夜色精品国产亚洲aⅴ| 欧美午夜激情视频| 亚洲精品自拍偷拍| 亚洲国产日韩一区| 欧美性生交xxxxx久久久| 国产99久久精品一区二区 夜夜躁日日躁| 热re91久久精品国99热蜜臀| 国产suv精品一区二区| 欧美精品久久久久久久久| 欧美一级大片视频| 国产精品扒开腿做爽爽爽的视频| 91成人性视频| 日本精品一区二区三区在线| 久久91亚洲人成电影网站| 日本不卡免费高清视频| 亚洲精品成人免费| 日韩免费中文字幕| 国产成人av在线| 国产精品一区二区三区在线播放| 亚洲一区二区少妇| 欧美日韩国产区| 久久视频这里只有精品| 日韩精品视频在线免费观看| 欧美精品18videosex性欧美| 国产精品吹潮在线观看| 国产精品狠色婷| 国模私拍一区二区三区| 国产精品直播网红| 91精品久久久久久久| 亚洲国产精久久久久久| 亚洲白拍色综合图区| 日韩国产欧美区| 亚洲国产古装精品网站| 久久99精品久久久久久琪琪| 国产一区二区三区中文| 日韩精品视频观看| 国产精品久久不能| 97超级碰在线看视频免费在线看| 国产一区二区三区中文| 亚洲一区二区久久| 亚洲精品国产suv| 91国内产香蕉| 中文字幕视频在线免费欧美日韩综合在线看| 亚洲精选一区二区| 日韩在线观看电影| 日韩在线播放一区| 亚洲在线第一页| 久久久久久91香蕉国产| 国内精品免费午夜毛片| 中文字幕在线看视频国产欧美在线看完整| 疯狂做受xxxx欧美肥白少妇| 成人激情视频在线| 久久精品视频va| 国产免费亚洲高清| 欧美日韩成人免费| 欧美成人免费在线视频| 欲色天天网综合久久| 日韩高清av在线| 国产精品视频白浆免费视频| 热久久免费国产视频| 国产女人18毛片水18精品| 国产精品草莓在线免费观看| 欧美国产一区二区三区| 91国产美女在线观看| 日韩中文在线中文网在线观看| 欧美大尺度激情区在线播放| 国产日韩欧美91| 亚洲人午夜色婷婷| 久久999免费视频| 日韩中文字幕精品视频| 中文字幕亚洲欧美日韩高清| 成人免费自拍视频| 亚洲精品日韩激情在线电影| 欧美日韩福利在线观看| 色偷偷9999www| 草民午夜欧美限制a级福利片| 久久久成人av| 日韩在线观看你懂的| 97人人模人人爽人人喊中文字| 久久久久久久久久久久久久久久久久av| 亚洲丁香久久久| 国产精品小说在线| 国产91在线视频| 成人黄色av网站| 91香蕉嫩草影院入口| 国产亚洲精品久久久优势| 91精品国产综合久久久久久蜜臀| 少妇久久久久久| 国产精品日韩一区| 欧美国产乱视频| 日韩欧美精品中文字幕| 日韩欧美国产免费播放| 欧美大荫蒂xxx| 色综合老司机第九色激情| 欧美有码在线观看视频| 国产精品ⅴa在线观看h|