<!DOCTYPE html>
<html>
<head>
<title>01_define.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
//函数的第一种定义方式
function fn1(){
console.log('fn1');
}
//函数就是一个非常特殊的对象,是一个Function类的实例,其实在内存中存储的操作是通过一个键值对来存储的
console.log(typeof fn1);
//由于函数是一个对象,可以通过以下方式定义
//以下是通过函数的拷贝来完成赋值(即在内存的栈中,fn1指向了堆中的一个方法,然后堆中fn1指向的方法在复制一份,由栈中的fn2指向它),两个引用并没有指向同一个对象,
var fn2 = fn1;
fn2();
fn1();
fn1 = function(){
console.log('fnnnn1');
}
/**
*函数虽然是一个对象,但是却和对象有一些区别,对象是通过引用的指向来完成对象赋值的,而函数却是通过对象的拷贝来完成的,
*所以fn1虽然变了,但不影响fn2
*/
fn2();
fn1();
//对于对象而言,是通过引用的指向来完成赋值,此时修改o1或者o2会将两个值都完成修改
var o1 = new Object();
var o2 = o1;
o2.name = 'evy';
console.log(o1.name);
o1.name='lin';
console.log(o2.name);
</script>
</head>
<body>
This is my HTML page. <br>
</body>
</html>
JS 函数的定义(一)
最新推荐文章于 2021-01-27 03:18:37 发布