窮小子

          MajorYe
          數(shù)據(jù)加載中……
          object.property與object["property"]的區(qū)別
          剛吃完飯,也沒啥事干,來(lái)寫寫blog吧
          也許就像你看到的那樣,我寫的東西是比較偏的
          先來(lái)舉個(gè)例子吧:
          >>> var person=function(){}
          >>> person.aa="aa"
          "aa"
          >>> person.bb="bb"
          "bb"
          >>> person.cc="cc"
          "cc"
          上面是定義了一個(gè)person類
          給這個(gè)類添加了幾個(gè)類屬性
          你單獨(dú)運(yùn)行

          >>> person.cc
          "cc"
          那是沒問題的
          但是你在程序中寫就有問題了,
          看看下面的程序:
          for(var t in person){
          alert(t);
          alert(person.t)  //為什么這個(gè)就有問題呢,結(jié)果為undefined
          }
          但該為
          for(var t in person){
          alert(t);
          alert(person.[t])  //這樣就可以了
          }
          為什么呢????

          The important difference to note between these two syntaxes is that in the first, the property name is an identifier, and in the second, the property name is a string. You'll see why this is so important shortly.

          In C, C++, Java, and similar strongly typed languages, an object can have only a fixed number of properties, and the names of these properties must be defined in advance. Since JavaScript is a loosely typed language, this rule does not apply: a program can create any number of properties in any object. When you use the . operator to access a property of an object, however, the name of the property is expressed as an identifier. Identifiers must be typed literally into your JavaScript program; they are not a datatype, so they cannot be manipulated by the program.

          On the other hand, when you access a property of an object with the [] array notation, the name of the property is expressed as a string. Strings are JavaScript datatypes, so they can be manipulated and created while a program is running.
          還沒寫完,待我醒來(lái)再細(xì)說(shuō)!

          posted on 2008-04-21 12:19 MajorYe 閱讀(386) 評(píng)論(1)  編輯  收藏 所屬分類: WEB開發(fā)

          評(píng)論

          # re: object.property與object["property"]的區(qū)別 2008-04-23 13:56 fuyongjie

          葉師兄:近來(lái)可好?
          太巧了,一中午正在研究firebug咋調(diào)試Js呢,這不正好就給碰上了。一下子就給入門了,哈哈哈


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 罗平县| 绥滨县| 长兴县| 碌曲县| 安阳县| 河北省| 临颍县| 宝兴县| 大庆市| 施秉县| 平罗县| 辽源市| 枣阳市| 平乐县| 丹棱县| 富宁县| 正安县| 儋州市| 马山县| 台山市| 奉新县| 根河市| 任丘市| 普陀区| 静海县| 常宁市| 玉山县| 鄯善县| 东莞市| 杭州市| 南郑县| 普宁市| 胶南市| 刚察县| 靖安县| 贺兰县| 崇文区| 固镇县| 襄城县| 东兰县| 甘德县|