窮小子

          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 閱讀(382) 評論(1)  編輯  收藏 所屬分類: WEB開發(fā)

          評論

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

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

            回復(fù)  更多評論    
          主站蜘蛛池模板: 奈曼旗| 双桥区| 左权县| 兴化市| 望江县| 亚东县| 衡阳县| 会宁县| 淳安县| 永昌县| 秦安县| 唐海县| 资讯 | 淳化县| 荆州市| 黄冈市| 封丘县| 阳信县| 安达市| 高清| 定西市| 安康市| 汨罗市| 巢湖市| 玉环县| 天镇县| 凤山市| 麻城市| 永济市| 桑日县| 漳州市| 屏东市| 漳平市| 大足县| 吉木萨尔县| 特克斯县| 拜泉县| 松滋市| 珠海市| 增城市| 施秉县|