第二篇:变量类型转换和函数

来源:互联网 发布:阿里云 域名 cname 编辑:程序博客网 时间:2024/06/05 07:55

我们经常看到typeof();可能有人就会问,那它的对象是谁呢?下来就聊聊它
js中前面的对象不用写的两种情况:
1.Global对象(js内部对象)
● typeof();
● parseInt();
● parseFloat();
● eval();
● Number();
● String();
● Boolean();
2.window对象(浏览器提供对象)
● alert();

一、变量类型转换:
1)整形—->字符串

     num = 10;    str = String(num);    alert(typeof(num));    alert(typeof(str));

2 )字符串—->整形

    str = '10px';    num = parseInt(str);    alert(typeof(str));    alert(typeof(num)+"," + num);

3 ) 所有类型—>布尔类型

      v = '';    b = !!(v);    alert(b); // false

4)所有类型->布尔类型(为假的情况)

    1)字符串('')    2)整型(0)    3)浮点型(0.0)    4)null    5)NaN    6)undefined

5)json字符串转json对象

  v = "{'username':'user1','age':'20'}";    obj = eval('('+v+')');    alert(obj);

函数分为普通函数和匿名函数
普通函数

function show(){}

匿名函数

obj.say = function(){}

下面是乘法表:

function out(n){for(i=1;i<=n;i++){    document.write('<h3>');    for(j=1;j<=i;j++){        document.write('<span>'+j+'x'+i+'='+(j*i)+'</span> ');    }    document.write('</h3>');}document.write('<hr>');}out(3);

消息框:
警告框:

alert();

确认框:

confirm();    boo = confirm('你确认吗?'); // 这个函数是由返回值的,当我们单击确认按钮时,返回true,取消按钮时,返回false    alert(boo); 

提示框:

prompt();
原创粉丝点击