《悟透javascript》摘抄1.3-一首小诗

6 篇文章 0 订阅
1 篇文章 0 订阅

到底什么是对象?

function Sing(){
       alert(Sing.author+": "+Sing.poem)
   };
   Sing.author="李白";
   Sing.poem="床前明月光,疑是地上霜。举头望明月,低头思故乡";
   Sing();
   Sing.author="刘刚";
   Sing.poem="hand in hand /day to day/till the end /shall we stay.";
       Sing();

函数是特殊的对象。

看你能否猜的出是哪句诗词?

hand in hand day to day till the end shall we stay

但是下面的代码又让人疑惑了。
第一段代码是:

<script>
    var anObject={};//定义一个变量
    anObject.aProperty="Property of Object";//定义属性
    anObject.aMethod=function(){
        alert("Method of Object")
    }//定义属性,属性恰好是方法
    alert(anObject["aProperty"]);//"Property of Object"
    anObject["aMethod"]();//alert("Method of Object")
    for(var s in anObject){
        alert(s+" is a "+typeof(anObject[s]))
    }
    //分别alert:
    //aProperty is a string;
    //aMethod is a function;
</script>
<script>
    var aFunction=function(){};
    aFunction.aProperty="Property of Object";
    aFunction.aMethod=function(){alert("Method of Object")};
    alert(aFunction["aProperty"]);
    alert(aFunction["aMethod"]);
    for(var s in aFunction){
        alert(s+" is a "+typeof(aFunction[s]))
    }
</script>

总结:对象和函数is array-alike;可以用属性名或方法名作为下表来访问;
数组:线性数据结构,有一定规律,适合进行统一的批量迭代操作;
对象:离散型数据结构,适合描述分散和个性化的东西;
javascript里面的对象到底是数组,还是对象?

空闲时间看了一下软文:讲的是技术学习的路线,当然谈的更多的是规划:
http://blog.csdn.net/wemedia/details.html?id=38990

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liugang0605

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值