一、原始类型和引用类型
JavaScript是以对象为核心的语言。
数据类型的分类:
1.原始类型——保存简单的数据值
boolean number string null undefined
2.引用类型——引用值则作为一个指针保存在变量对象内,该指针指向实际对象在内存中的存储位置
<script>
var a=[1,2,3];
var b=[];
for(var i=0;i< a.length;i++){
b[i]=a[i];
}
console.log(b);
</script>
1.鉴别原始类型(typeof)
<script>
/*typeof*/
var num=10;
var str="10";
console.log(num === str)
</script>
2.原始方法
<script>
var str="Hello World";
var lower = str.toLowerCase();
var upper = str.toUpperCase();
var char = str.charAt(2);
var sub =str.substring(0,4);
console.log(lower);
console.log(upper);
console.log(char);
console.log(sub); //0<=sub<4;
var count = 10.535325;
var fixed = count.toFixed(3);
console.log(fixed)
</script>
3.引用类型
<script>
var anqi = new Object();
anqi.name="chengchao";
anqi.age=24;
console.log(anqi);
</script>
4.鉴别引用类型和数组
<script>
var arr=[1,2,3];
var object = {};
function anqi(){
}
// 鉴别引用类型
console.log(anqi instanceof Function); //true
console.log(anqi instanceof Object); //true
console.log(object instanceof Object); //true
console.log(object instanceof Array); // false
console.log(arr instanceof Object); //true
console.log(arr instanceof Array); //true
// 鉴别数组
console.log(Array.isArray(arr))
</script>
5.原始封装类型
<script>
var name = "Anqi";
var temp = new String(name);
var first = name.charAt(0);
temp = null;
name.last = "Mimi";
// console.log(first); // A
console.log(name.last); // undefined
</script>
网友评论