您的位置:首页 > Web前端 > JavaScript

js基础巩固之--变量类型以及声明

2015-02-02 22:32 274 查看
在只求达到目的的同时,却丢失了坚实的基础,深知自己的基础有待加强。目前主要学习的书籍是《javascript高级编程设计(第三版)》,本博文笔记是第三章第三小节--数据类型(p23)。

本博文用到的知识点:变量的声明和赋值,变量的类型的检测(typeof),对象的遍历(for in),JS中arguments对象。

本文宗旨:总结所学到的知识点,得到大神的指点/纠错,给予和我一样的菜鸟一些帮助!

线上代码演示地址:http://runjs.cn/code/n9chq2bo

JScode

window.onload = function(){

var obj = {//声明测试对象
_string     : "字符串",
_number1    : 12345,
_number2    : 1.3,
_boolean    : true,
_undefined  : undefined,
_null       : null,
_array1     : new Array(3),
_array2     : [],
_array3     : [1,2,3,4],
_object1    : new Object(),
_object2    : {},
},
objTypeof = {};//声明测试类型对象

for(var x in obj){
objTypeof[x] = typeof(obj[x]);//注意,typeof是操作符,并不是方法
//以上代码同等于
//objTypeof[x] = typeof obj[x];
}

for( var index in obj){
document.write(index+'------'+obj[index]+'</br>');
}
document.write('=========================</br>');

for( var index in objTypeof){
document.write(index+'------值类型为:'+objTypeof[index]+'\n </br>');
}
document.write('=========================</br>');

Write('五种基本数据类型','Undefined','Null','Boolean','NUmber','String');
function Write(){
for(var index in arguments){//arguments对象,是JS隐式对象,不能显式声明,函数执行时才可用
document.write(arguments[index]+',');
}
}
//访问对象属性的两种方式,注意第二种是中括号里字符串
console.log(obj._string);
console.log(obj['_string']);

//访问没有定义的变量
//console.log(aaa);//直接报错

var bbb;//声明未赋值
console.log(bbb);//返回undefined

}
努力一点,生活更美好!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: