JavaScript 【第二章 对象】

JavaScript中的对象是有着属性和方法的一种特殊数据类型。

常见的对象有数字Number,字符串String,日期Date,数组Array等

一、数字

1、创建数字对象

<script>
var x = new Number(123);
 document.write('数字对象x的值:'+x);
 document.write("<br>");
 document.write('数字对象x的类型:'+typeof x); //通过typeof 获知这是一个object
 document.write("<br>");
var y = 123;
 document.write('基本类型y的值:'+y);
 document.write("<br>");
document.write('基本类型y的类型:'+typeof y); //通过typeof 获知这是一个number
</script>

 

二、字符串

1、创建字符串

<script>
var x = "5";
var y = new String(x);
document.write("变量x的值是:"+x);
document.write("<br>");
document.write("变量x的类型是:"+(typeof x));
document.write("<br>");
document.write("变量y的值是:"+y);
document.write("<br>");
document.write("变量y的类型是:"+(typeof y));
document.write("<br>");
</script>

 

2、字符串长度

<script>
var y = new String("Hello JavaScript");
document.write("通过.length属性获取字符串'Hello JavaScript'的长度"+y.length);
</script>

 

3、返回指定位置字符

<script>
var y = new String("Hello JavaScrpt");
document.write("字符串y的值:"+y);
document.write("<br>");
document.write('通过 charAt(0)获取位置0的字符串: '+y.charAt(0)); //返回H
document.write("<br>");
document.write('通过 charCodeAt(0)获取位置0的字符的 Unicode码 :'+y.charCodeAt(0)); //返回H对应的Unicode码 72
</script>

 

4、字符串拼接

<script>
var x = new String("Hello ");
var y = new String("Javascript");
document.write( '通过函数concat()把x和y连接起来: ' +  x.concat(y) );
</script>

 

5、子字符串出现位置

<script>
var y = new String("Hello JavaScript");
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write('通过 indexOf ("a")获取子字符"a" 第一次出现的位置 '+y.indexOf ("a"));
document.write('<br>');
document.write('通过 lastIndexOf ("a")获取子字符"a" 最后出现的位置 '+y.lastIndexOf ("a"));
</script>

 

6、字符串比较

<script>
   
var x = new String("Hello");
var y = new String("Hello");
var z = new String("aloha");
   
document.write( '字符串x的值: '+x);
document.write('<br>');
document.write( '字符串y的值: '+y);
document.write('<br>');
document.write( '字符串z的值: '+z);
document.write('<br>');
 
document.write('通过 localeCompare()判断 x和y是否相等 '+x.localeCompare(y));
document.write('<br>');
document.write('通过 localeCompare()判断 x和z是否相等 '+x.localeCompare(z));
document.write('<br>');
 
document.write('0 表示相等<br>');
document.write('1 表示字母顺序靠后<br>');
document.write('-1 表示字母顺序靠前<br>');
 
</script>

7、字符串截取

<script>
   
var x = new String("Hello JavaScript");
 document.write( '字符串x的值: '+x);
document.write('<br>');
document.write('x.substring (0,3) 获取位0到3的字符串: '+x.substring (0,3) );
document.write('<br>');
document.write('左闭右开,取得到0,取不到3');
 
</script>

 

8、根据分隔符,转换为数组

<script>
var x = new String("Hello This Is JavaScript");
document.write( '字符串x的值: '+x);
document.write('<br>');
 
var y =  x.split(" ");
document.write('通过空格分隔split(" "),得到数组'+y);
document.write("<br>");
 
var z =  x.split(" ",2);
document.write('通过空格分隔split(" ",2),得到数组,并且只保留前两个'+z);
  
</script>

 

9、替换子串

x.replace(/a/g, "o");

或者

var regS = new RegExp("a","g");

x.replace(regS, "o");

 

 

10、返回类型

charAt返回的值H

其类型是string

concat返回的值Hello JavaScript!!!

其类型是string

substring返回的值Hello

其类型是string

 

三、数组

js中数组是动态(长度可变化)

1、创建数组对象

创建一个数组对象

创建数组对象的3种方式:

1. new Array() 创建长度是0的数组

2. new Array(5); 创建长度是5的数组,,但是其每一个元素都是undefine

3. new Array(3,1,4,1,5,9,2,6); 根据参数创建数组

 

2、数组长度

<script>
function p(s){
  document.write(s);
  document.write("<br>");
}
var x = new Array(3,1,4,1,5,9,2,6); //根据参数创建数组
p('当前数组是:'+x);
p('通过.length获取当前数组的长度:'+x.length);
</script>

 

3、数组字符串表达

<script>
function p(s){
  document.write(s);
  document.write("<br>");
}
 
var x = new Array(3,1,4);
p('数组x是:'+x);
var y = x.join();
p('y = x.join() 得到的是数组x的字符串表达,其值是'+y+" 其类型是 :" +(typeof y));
var z = x.join("@");
p('z = x.join("@");是x的字符串表达,不过分隔符不是默认的"," 而是"@" : '+z);
 
</script>

 

4、push、pop

<script>
function p(s){
  document.write(s);
  document.write("<br>");
}
 
var x = new Array(3,1,4);
p('数组x是:'+x);
 
x.push(5);
p('向x中push 5,得到 ' + x);
var e = x.pop();
p('从x中pop一个值出来,其值是 ' + e);
 
p('pop之后,x数组的值是:'+x);
 
</script>

 

5、数组排序

<script>
function p(s){
  document.write(s);
  document.write("<br>");
}
 
var x = new Array(3,1,4,1,5,9,2,6);
p('数组x是:'+x);
x.sort();
p('使用sort排序后的数组x是:'+x);
 
</script>

 

6、自定义排序

<script>
function p(s){
  document.write(s);
  document.write("<br>");
}
 
function comparator(v1,v2){
   return v2-v1;//v2-v1表示大的放前面,小的放后面
}
 
var x = new Array(3,1,4,1,5,9,2,6);
p('数组x是:'+x);
x.sort(comparator);
p('使用sort 进行自定义倒排序后的数组x是:'+x);
 
</script>

 

7、获取子数组

方法 slice 获取子数组

注意: 第二个参数取不到

 

8、删除和插入元素

方法 splice (不是 slice) 用于删除,插入数组中的元素

数组x是:3,1,4,1,5,9,2,6

x.splice (3,2) 表示从位置3开始 ,删除2个元素:3,1,4,9,2,6

x.splice(3,0,1,5) 从位置3开始,删除0个元素,但是插入1和5,最后得到:3,1,4,1,5,9,2,6

 

四、日期

JavaScript使用Date对象表示日期

1、创建日期对象

<script>
  var d = new Date();
  document.write('new Date():'+d);
</script>

 

2、获取年月日

<script>
  var d = new Date();
  document.write('分别获取年月日: ');
  document.write(d.getFullYear());
  document.write("/");
  document.write(d.getMonth()+1);
  document.write("/");
  document.write(d.getDate());
</script>

 

3、获取时分秒

<script>
  var d = new Date();
  document.write("分别获取时:分:秒:毫秒 ");
  document.write(d.getHours());
  document.write(":");
  document.write(d.getMinutes());
  document.write(":");
  document.write(d.getSeconds());
  document.write(":");
  document.write(d.getMilliseconds());
</script>

 

4、获取一周第几天

<script>
var day=new Date().getDay(); //通过日期对象获取数字形式的星期几
var weeks = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
 
document.write("今天是 : "+weeks[day]);
  
</script>

 

5、设置日期对象时间

<script>
var d=new Date();
document.write("修改日期对象的值为世界末日:<br>");
d.setFullYear(2012);
d.setMonth(11); //月份是基0的,所以11表示12月
d.setDate(12);
 
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
 
document.write(d);
</script>

 

五、Math对象

1、自然对数和圆周率

<script>
document.write(Math.E);
document.write("<br>");
document.write(Math.PI);
</script>

 

2、绝对值

<script>
document.write(Math.abs(-1));
</script>

 

3、最大值最小值

<script>
document.write(Math.min(1,100));
document.write("<br>");
document.write(Math.max(1,100));
</script>

 

4、求幂

<script>
document.write(Math.pow(3,3)); //3的立方,即27
</script>

 

5、四舍五入

<script>
document.write(Math.round(3.4));
document.write("<br>");
document.write(Math.round(3.5));
</script>

 

6、随机数

<script>
document.write("一个 0-1 之间的随机数 : Math.random():");
document.write("<br>");
document.write(Math.random());
document.write("<br>");
document.write("十个 5-10 之间的随机数 : Math.round(Math.random() *5)+5 ");
document.write("<br>");
for(i=0;i<10;i++){
document.write(Math.round(Math.random() *5)+5 ); //5-10之间的随机整数
document.write("<br>");
}
</script>

 

六、自定义对象

1、创建对象

<script>
var hero = new Object();
hero.name = "盖伦"; //定义一个属性name,并且赋值
hero.kill = function(){
  document.write(hero.name + " 正在杀敌" ); //定义一个函数kill
}
hero.kill(); //调用函数kill  
</script>

 

2、设计一个对象

<script>
function Hero(name){
  this.name = name;
  this.kill = function(){
     document.write(this.name + "正在杀敌<br>");
  }
}
 
var gareen = new Hero("盖伦");
gareen.kill();
 
var teemo = new Hero("提莫");
teemo.kill();
  
</script>

 

3、存在对象,增加方法

通过prototype实现

<script>
function Hero(name){
  this.name = name;
  this.kill = function(){
     document.write(this.name + "正在杀敌<br>");
  }
}
var gareen = new Hero("盖伦");
gareen.kill();  
var teemo = new Hero("提莫");
teemo.kill();
Hero.prototype.keng = function(){
  document.write(this.name + "正在坑队友<br>");
}
gareen.keng();
teemo.keng();
</script>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值