当前位置:网站首页>JS中的原型链面试题--Foo和 getName
JS中的原型链面试题--Foo和 getName
2022-06-25 22:09:00 【wen_文文】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function Foo() {
getName = function(){ console.log(1); };
return this;
}
Foo.getName = function() { console.log(2); };
Foo.prototype.getName = function(){ console.log(3); };
var getName = function() { console.log(4); };
function getName(){ console.log(5); };
Foo.getName(); //2
getName(); //4 执行全局对象的getName方法, 函数声明会先于表达式执行,所以全局对象的getName为 console.log(4)
Foo().getName(); //1 Foo函数中将全局对象window上的getName方法修改为console.log(1)了
getName(); //1 相当于执行window.getName()
new Foo.getName(); // 2
new Foo().getName(); //3 new Foo()时执行Foo函数,返回实例对象,由于Foo()本身并没有this.getName()的方法,所以调用原型对象上的方法
// 函数声明会先于函数表达式执行
// sum1(1,2)
// function sum1(a,b){return a+b;}
// sum2(3,4) //报错:Uncaught TypeError: sum2 is not a function at 1.原型和原型链_字节跳动.html:31
// var sum2 = function(a,b) {return a+b;}
</script>
</body>
</html>将Foo函数做稍微一点改变后,继续打印的结果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function Foo() {
// 这里加了一个this
this.getName = function(){ console.log(1); };
return this;
}
Foo.getName = function() { console.log(2); }; // 相当于给Foo函数对象上添加了一个实例属性getName
Foo.prototype.getName = function(){ console.log(3); };
var getName = function() { console.log(4); };
function getName(){ console.log(5); };
Foo.getName(); // 2
getName(); // 4
Foo().getName(); // 1 此时this指向window,相当于修改全局对象上的getName方法为console.log(1)
getName(); // 1 调用全部对象上的getName方法
new Foo.getName(); // 2
new Foo().getName(); // 1 new Foo()返回一个实例对象,此时实例对象上有this.getName方法,所以就直接调用,返回1
</script>
</body>
</html>
边栏推荐
- YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
- Qtcreator formatting code
- 一文讲透研发效能!您关心的问题都在
- Binary, hexadecimal, big end and small end
- OpenResty篇01-入门简介和安装配置
- c_ uart_ interface_ Example and offboard modes
- 如何进行流程创新,以最经济的方式提升产品体验?
- String object (constant) pool
- Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]
- Online customer service - charging standards and service provision of third parties
猜你喜欢

Online customer service - charging standards and service provision of third parties

Gradle的环境安装与配置

idea 查看单元测试覆盖率

录屏转gif的好用小工具ScreenToGif,免费又好用!

UE4 learning records create a role and control its movement

Hbuilderx uses the gaude map to obtain the current location

C# IO Stream 流(二)扩展类_封装器

Summary of common JDBC exceptions and error solutions

史上最简单的录屏转gif小工具LICEcap,要求不高可以试试

产品经理如何把控产品开发的进度
随机推荐
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
CXF
聊聊swoole或者php cli 进程如何热重启
C2. k-LCM (hard version)-Codeforces Round #708 (Div. 2)
Visual studio code create minimal web API (asp.net core)
Using Google protobuf protocol environment configuration in PHP
Uniapp - call payment function: Alipay
Gradle的环境安装与配置
Solve 'tuple' object has no attribute 'lower‘
Talk about how to hot restart a spoole or PHP cli process
Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
Blob
.user.ini文件导致的php网站安装问题
Stream in PHP socket communication_ Understanding of select method
Leetcode-1528- rearrange string - hash table - string
php进程间传递文件描述符
Hibernate core api/ configuration file / L1 cache details
Qtcreator formatting code
为什么Integer的比较最好使用equals
Download the latest V80 version of Google Chrome