一、创建对象并将其初始化

 a、使用new创建对象和方法

<<!DOCTYPE html>
<html>
<head>
<mete http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>javaScript中的数组和对象</title>
<script type="text/javascript" src="./out.js">
var box=new Object;
box.name="张三";
box.age=23;
box.run=running();
function running(){
return "我是中国人";
}
document.write(typeof box+"<br/>");
document.write(box.name +"<br/>");
document.write(box.age +"<br/>");
document.write(box.run);
</script>
</head>
<body> </body>
</html>

  b、字面量表示方法

<<!DOCTYPE html>
<html>
<head>
<mete http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>javaScript中的数组和对象</title>
<script type="text/javascript" src="./out.js">
var box={
name:"张三",
age:,
run:function(){
return "我是中国人!!"
}
};
document.write(typeof box);
document.write(box.name);
document.write(box.age);
document.write(box.run());
</script>
</head>
<body> </body>
</html>

  c、综合使用
  说白了就是二者结合使用

function box(obj){
if(obj.name!=undefined)document.write(obj.name+"<br/>");
if(obj.age!=undefined)document.write(obj.age+"<br/>");
if(obj.love!=undefined)document.write(obj.love+"<br/>");
}
var obj={
name:"张三",
age:23
};
box(obj);

二、Array类型

新建数组的三种方法:

var box=new Array(1,2,3,4);
document.write(typeof box);
document.write(box); var box2=new Array(10);
box2[3]=4;
box2[6]=25;
document.write(typeof box2);
document.write(box2); var box3=[1,3,4,44,5,5,23];
document.write(typeof box3);
document.write(box3);

(1)转换方法
       对象或数组都具有toLocaleString(),toString()和valueOf()方法。其中toString()和valueOf()无论重写了谁,都会返
回相同的值。数组会将每个值进行字符串形式的拼接,以逗号隔开。

  默认的情况下,数组字符串都会以逗号隔开。如果使用join()方法可以使用不同的分割符来构建这个字符串

(2)栈方法
       ECMAScript数组提供了一种让数组的行为类似于其他数据结构的方法。也就是说,可以让数组像栈一样,可以限
制插入和删除想的数据结构。栈是一种后进先出的数据结构,也就是最新添加的元素最早被移除。而栈元素的插入和
移除,只发生在栈的顶部。ECMAScript为数组专门提供了push()和pop()方法
       栈操作数组元素的图片:

push()方法可以接受任意数量的参数,把它们逐个添加到数组的末尾,并返回修改数组的长度。而pop()方法则从
数组末尾移除最后一个元素,减小数组的length值,然后返回移除的元素。

1
2
3
4
5
6
7
8
9
10
var box=[1,2,3,4];
document.write(box+"<br/>");
box.push(5,6);//在数组末尾添加元素
document.write(box+"<br/>");
document.write(box.push(7,8)+"<br/>");//在数组末尾添加元素,并返回添加元素后数组的长度
document.write(box+"<br/>");
box.pop();//移除数组末尾的元素
document.write(box+"<br/>");
document.write(box.pop()+"<br/>");//移除数组末尾的元素,并返回移除的元素
document.write(box);

输出:

(3)队列方法
       栈方法是后进先出,队列方法是先进先出。队列在数组的末端添加元素,从数组的前端移除元素。通过push()向
数组末端添加一个元素,然后通过shift()方法从数组的前端移除一个元素。
       队列操作数组元素的图片

1
2
3
4
5
6
7
8
9
10
var box=[1,2,3,4];
document.write(box+"<br/>");
box.push(5,6);//在数组末尾添加元素
document.write(box+"<br/>");
document.write(box.push(7,8)+"<br/>");//在数组末尾添加元素,并返回添加元素后数组的长度
document.write(box+"<br/>");
box.shift();//移除数组前端的一个元素
document.write(box+"<br/>");
document.write(box.shift()+"<br/>");//移除数组前端的一个元素,并返回移除的元素
document.write(box);

输出:

ECMAScript还为数组提供了一个unshift()方法,它和shift()方法的功能完全相反。unshift()方法为数组的前端添加
一个元素。

1
2
3
4
5
6
7
8
9
10
var box=[1,2,3,4];
document.write(box+"<br/>");
box.unshift(0);//在数组的前端添加一个元素
document.write(box+"<br/>");
document.write(box.unshift(-1)+"<br/>");//在数组的前端添加一个元素,并返回添加元素会数组的长度
document.write(box+"<br/>");
box.pop();//在数组末尾移除元素
document.write(box+"<br/>");
document.write(box.pop()+"<br/>");//在数组末尾移除元素,并返回移除元素后数组的长度
document.write(box);

输出:

(4)重排序方法
数组中已经存在两个直接用来排序的方法:reverse()和sort()。
reverse():逆向排序

1
2
3
4
var box=[1,2,3,4,5];
box.reverse();
document.write(box+"<br/>");//输出54321
document.write(box.reverse());//再次进行逆序,输出12345

sort():从小到大排序

1
2
3
4
var box=[3,2,6,4,1,5];
box.sort();
document.write(box+"<br/>");//输出1,2,3,4,5,6
document.write(box.sort());//再次从小到大进行排序

如果我们实验次数多的话可能回遇到这样的问题,

1
2
3
var box=[0,15,10,1,5];
box.sort();
document.write(box);//输出0,1,10,15,5

我们从结果可以看出,这违背了我们想要的结果,解决方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function compare(value1,value2){
  if(value1<value2){
   return -1;
  }
  else if(value1>value2){
   return 1;
  }
  else{
   return 0; 
  
}
var box=[0,15,10,1,5];
box.sort(compare);
document.write(box);//输出0,1,5,10,15

 (5)操作方法
JS为操作已经包含在数组中的元素提供了许多的方法。concat()方法可以基于当前数组创建一个新数组。slice()方
法可以基于当前数组获取指定区域元素并创建一个新数组。splice()方法主要用途是向数组的中部插入元素。
 a

1
2
3
4
var box=[1,2,3,4,5];
var box1=box.concat(6);//创建新数组,并添加新元素
document.write(box1+"<br/>");//输出1,2,3,4,5,6,
document.write(box);//原数组不变化

b

1
2
3
4
var box=[1,2,3,4,5];
var box1=box.slice(2);//取出索引为2以后的元素组成新的数组
document.write(box1+"<br/>");//输出3,4,5
document.write(box);//原数组不变化

c

1
2
3
4
var box=[1,2,3,4,5];
var box1=box.slice(2,3);//取出索引为2到3之间的元素组成新的数组
document.write(box1+"<br/>");//输出3
document.write(box);//原数组不变化

splice中的删除功能

1
2
3
4
var box=[1,2,3,4,5];
var box1=box.splice(0,2);//截取索引为0开始的两个元素组成新的数组
document.write(box1+"<br/>");//返回截取的元素1,2
document.write(box);//当前数组被截取的元素被删除,输出3,4,5

splice中的插入功能

1
2
3
4
var box=[1,2,3,4,5];
var box1=box.splice(4,0,6);//索引为4的位置插入了一个元素
document.write(box1+"<br/>");//返回新的数组为空,并没有截取元素
document.write(box);//当前数组索引为4的位置插入一个元素1,2,3,4,6,5

splice中的替换功

1
2
3
4
var box=[1,2,3,4,5];
var box1=box.splice(4,1,6);//索引为4的元素被替换,替换下来的元素组成新数组
document.write(box1+"<br/>");//返回新的数组5
document.write(box);//被替换后的原数组1,2,3,4,6

以上就是关于JavaScript对象和数组的详细介绍,希望对大家的学习有所帮助。

JavaScript中数值和对象的更多相关文章

  1. JavaScript中的事件对象

    JavaScript中的事件对象 JavaScript中的事件对象是非常重要的,恐怕是我们在项目中使用的最多的了.在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含这所有与事件有 ...

  2. Javascript学习1 - Javascript中的类型对象

    原文:Javascript学习1 - Javascript中的类型对象 1.1关于Numbers对象. 常用的方法:number.toString() 不用具体介绍,把数字转换为字符串,相应的还有一个 ...

  3. JavaScript中创建字典对象(dictionary)实例

    这篇文章主要介绍了JavaScript中创建字典对象(dictionary)实例,本文直接给出了实现的源码,并给出了使用示例,需要的朋友可以参考下 对于JavaScript来说,其自身的Array对象 ...

  4. 简单使用JSON,JavaScript中创建 JSON 对象(一)

    JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  5. JavaScript中的window对象

    JavaScript中的window对象:http://www.cnblogs.com/kissdodog/archive/2013/01/01/2841464.html

  6. js:JavaScript中的ActiveXObject对象

    JavaScript中的ActiveXObject对象作用: https://blog.csdn.net/pl1612127/article/details/77862174

  7. 详解javascript中的this对象

    详解javascript中的this对象 前言 Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象.Javascript可以通过一定的 ...

  8. javascript中如何获取对象名

    javascript中如何获取对象名 一.总结 一句话总结:将对象传入参数,看参数是否为函数(js中的对象和函数是一个意思么(函数肯定是对象)),对象参数.name属性即可获得 //版本4 funct ...

  9. JavaScript中的global对象,window对象以及document对象的区别和联系

    JavaScript中的global对象,window对象以及document对象的区别和联系 一.概念区分:JavaScript中的global对象,window对象以及document对象 1.g ...

随机推荐

  1. R语言入门基础

    教程:常用运算函数.对一般数据进行运算的常用函数: 1.round() #四舍五入 例:x <- c(3.1416, 15.377, 269.7) round(x, 0) #保留整数位 roun ...

  2. 企业环境中部署 ActiveMQ

    这一章讲述了怎么配置 ActiveMQ 集群.

  3. 安装SQL 2008失败 (win7 旗舰版 32位)

    本机系统 win7 32位 旗舰版 机器已经有sql 2005了,2008 不能安装成功,而且无任何错误提示. 那么通过windows install clean up (下载 windows ins ...

  4. java常用封装方法

    public Map<String,String> getDateByStr(String str,String startDate,String endTime){ Map<Str ...

  5. mac上完整卸载删除:android studio方案

    如果你是mac  ,你删除as ,删不干净也正常,你会发现安装的时候,前面的东西也在.配置文件在,会导致你以前的错误不想要的东西都在. 废话不多说,复制粘贴!!~~~~~~~~ 第一步: 复制粘贴!! ...

  6. New Concept English three (47)

    Pollution is the price we pay for an overpopulated, over industrialized planet. When you come to thi ...

  7. 如何用visual studio2013编写简单C语言程序

    vc++6.0 作为经典版本,虽然已经几乎淘汰,但还是有很多的初学者在使用.但当他们使用vs2013时会发现界面和操作和vc++6.0有了极大的不同,不知该如何 操作.随着vs2013的普及,更多人使 ...

  8. 已知一个数组a[N]来构造数组b[N]的有趣算法题

    给定一个数组a[N],我们希望构造数组b[N],其中b[i]=a[0]*a[1]*...*a[N-1]/a[i].在构造过程要求满足:1.不使用除法:2.O(1)空间复杂度和O(n)时间复杂度:3.除 ...

  9. Python collections系列之有序字典

    有序字典(orderedDict ) orderdDict是对字典类型的补充,他记住了字典元素添加的顺序 1.创建一个有序字典 import collections dic = collections ...

  10. Python numpy函数:reshape()

    reshape()函数用于改变数组对象的形状: import numpy as np a = np.array([1,2,3,4,5,6,7,8]) #转换成2D数组 b = a.reshape((2 ...