给自己看的tips(更新中)

本文介绍了JavaScript中的一些关键概念和编程技巧,包括变量声明、函数类型、即时函数的应用等,并通过实例展示了如何正确使用这些特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-size:18px;"><span style="font-size:18px;">var x=020;
var y=parseInt('x');
alert(y);</span></span>

返回NaN,因为引号里面的内容一律被识别为字符串,也就是说引号里不存在变量。正确的声明方式为var y=parseInt(x);或var y=parseInt(x+'');两者等价。

另:单引号和双引号等价,除了嵌套的情况。单纯输出字符串的时候优先使用单引号,运行速度会更快。

<span style="font-size:18px;"><pre name="code" class="html"><span style="font-size:18px;"><script language="javascript">
    document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');
</script></span></span>

encodeURI和encodeURIComponet。上述是典型,前者不会编码/。但即使不加encodeURIComponent,对于浏览器来说都没有问题,难道是浏览器能够解析?通常浏览器地址栏显示的都是encodeURI后的地址

<span style="font-size:18px;">var test1=function(){return 3;};
alert(typeof test1);
alert(typeof test1());</span>

分别为function和number;即要利用test1参加运算,必须使用test();

JavaScript 是脚本语言。浏览器会在读取代码时,逐行地执行脚本代码。而对于传统编程来说,会在执行前对所有代码进行编译。

<span style="font-size:18px;"><script>
        function testFunction(x,y){
            return x*y;
        }
        var result=function(){return 3*4;};
        alert(result());
        function myFunction(){
            document.getElementById("demo").innerHTML=result();
        }
</script></span>
<pre><span style="font-size:18px;"><p id="demo">this is a test</p>
<button type="button" onclick="myFunction()">请点击这里</ button></span>


不要漏掉result()的(),漏掉的话result="function(){return 3*4}"这是个字符串。当然,下面的代码更方便

<span style="font-size:18px;"><script>
        function myFunction(){
            document.getElementById("demo").innerHTML=(function(x,y){return x*y}(3,4));
        }
</script></span>
<pre><pre><span style="font-size:18px;"><p id="demo">this is a test</p>
<button type="button" onclick="myFunction()">请点击这里</ button></span>



<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p>My First Paragraph.</p>

<button onclick="myFunction()">点击这里</button>

<script>
function myFunction()
{
document.write("糟糕!文档消失了。");
}
</script>

</body>
</html>
如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值