窮小子

          MajorYe
          數(shù)據(jù)加載中……
          object.property與object["property"]的區(qū)別
          剛吃完飯,也沒啥事干,來寫寫blog吧
          也許就像你看到的那樣,我寫的東西是比較偏的
          先來舉個(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.
          還沒寫完,待我醒來再細(xì)說!

          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

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 襄汾县| 元阳县| 延长县| 嘉祥县| 文成县| 黄浦区| 娄烦县| 吴堡县| 延川县| 兰州市| 郎溪县| 彭州市| 新乐市| 杭锦旗| 长丰县| 凤庆县| 内江市| 桂阳县| 九龙坡区| 侯马市| 奈曼旗| 报价| 乌鲁木齐市| 磴口县| 房产| 竹山县| 安新县| 日土县| 新乡县| 巴彦淖尔市| 中西区| 南昌县| 德格县| 图们市| 甘肃省| 彰化市| 灯塔市| 中山市| 井研县| 祥云县| 靖边县|