JavaScript---对象学习(一)object对象、string对象、javaScript的constructor属性和prototype属性

JavaScript对象学习

那么我们就先看看看object对象把

object对象学习
查看帮助文档 ,得到了这些信息

 Object 对象
提供所有 JScript 对象通用的功能。
obj = new Object([value]) 
两个属性
prototype 属性 | constructor 属性
三个方法
toLocaleString 方法 | toString 方法 | valueOf 方法

那么就来测试测试帮助文档的方法把(属性放到String对象里学 嘻嘻)
演示代码

<script type="text/javascript">
              var arr=[1,3,5,7];
    document.write("</br>"+arr);            
    document.write("</br>toString()");
    document.write("</br>"+arr.toString());
    document.write("</br>toLocaleString()");
    document.write("</br>"+arr.toLocaleString());
</script>

结果
这里写图片描述

恩,我们发现输出函数名默认会输出toString();
toLocaleString(); 具体我也没学很明白。。。看帮助文档把
toLocaleString 方法返回一个 String 对象,这个对象中包含了用当前区域设置的默认格式表示的日期。可能是按照日期格式来修改了输出吧。

string对象学习
查看帮助文档 ,得到了这些信息

  • 属性
    constructor 属性 | length 属性 | prototype 属性
  • 方法

anchor 方法 | big 方法 | blink 方法 | bold 方法 | charAt 方法 | charCodeAt 方法 | concat 方法 | fixed 方法 | fontcolor 方法 | fontsize 方法 | fromCharCode 方法 | indexOf 方法 | italics 方法 | lastIndexOf 方法 | link 方法 | match 方法 | replace 方法 | search 方法 | slice 方法 | small 方法 | split 方法 | strike 方法 | sub 方法 | substr 方法 | substring 方法 | sup 方法 | toLowerCase 方法 |toUpperCase 方法 | toString 方法 | valueOf 方法

String 对象的创建方式 (与Java类似)

// 试试
var str1 = "abc";
var str2 = new String("abc");

document.write("</br>第一种创建String对象方式---"+str1); document.write("</br>第一种创建String对象方式---"+str2);

应该都能成功
这里写图片描述

接下来演示一些方法

script type="text/javascript">
             var str1 = "abc";
document.write(str1);
document.write("</br>调用Sting对象的big方法"+str1.big());
 document.write("</br>调用Sting对象的bold方法"+str1.bold());
document.write("</br>调用Sting对象的charAt方法"+str1.charAt(0));
document.write("</br>调用Sting对象的fixed方法"+str1.fixed());
document.write("</br>调用Sting对象的fontcolor方法"+str1.fontcolor("red"));
document.write("</br>调用Sting对象的fontsize方法"+str1.fontsize("20"));
document.write("</br>调用Sting对象的toUpperCase方法"+str1.toUpperCase());

</script>

看看结果
这里写图片描述

发现好像这些方法都是HTML 里面控制 显示的一些属性,那想办法;看看他们是什么代码

<script type="text/javascript">
    var str1 = "abc";            
document.write(str1);
document.write("</br>调用Sting对象的big方法"+str1.big());
document.write("</br>调用Sting对象的toUpperCase方法"+str1.toUpperCase());

alert(str1.big());

</script>

猜猜结果
这里写图片描述

惊了!! 这就是HTML代码,用个标签包起来。。。恩,很好很好

接下来看看 他的属性吧

constructor 属性 | length 属性 | prototype 属性
constructor属性
先看一段代码

<script type="text/javascript">
    var str1 = "abc";
    alert(str1.constructor);
</script>

这里写图片描述

然后我们很开心的看到了他的代码,我们得到了一个信息
这里给我们提示一项技术:js中的对象是用function来做的,这个我们以后学

<script type="text/javascript">
     var str1 = "abc";
    alert(str1.constructor==String);
</script>

结果
这里写图片描述

这里我们可以明白 str1的原型是String 、、、因为JavaScript的所有对象都是原型可能出来的,所有constructor就是拿到对象原型的属性
原理图如下
这里写图片描述

prototype 属性
prototype :就是可以用来在原型里面添加东西的属性

那么我们来试试 加两个属性

<script type="text/javascript">
        //加个name属性
        String.prototype.name ="达哥";
        //加个age属性
        String.prototype.age ="20";
             var str1 = "abc";
        document.write("看看str1的name属性 :"+str1.name);
        document.write("</br>看看str1的age属性 :"+str1.age);
</script>

结果 当然是成功啦!!!
这里写图片描述

我们发现JavaScript中String的方法中没有trim()方法。我们来加一个把

trim()方法

字符串把两边的空格去掉

演示代码:

<script type="text/javascript">
        String.prototype.trim=function(){
            var start,end;
              start=0;
              end=this.length-1;
              while(start<=end && this.charAt(start)==' '){
                     start++;
              }
              while(start<=end && this.charAt(end)==' '){
                     end--;
              }
              return this.substring(start,end+1);
          };
          var  s = " a dsa  ";
          document.write(s.trim())
          alert("**"+s.trim()+"**");
        </script

结果,很好很好!!!
这里写图片描述

在String 原型中加了方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值