当前位置:网站首页>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>
边栏推荐
- Today's 61 Fu
- 平衡二叉树AVL
- util. Collection and encapsulation of JS tool functions
- unsigned与signed之大白话
- mongodb
- The package name of the manifest file in the library project and the app project are not the same
- 1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
- Leetcode 513. 找树左下角的值
- 数据同步
- 两种块级元素居中的方式
猜你喜欢

Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or

解析产品开发失败的5个根本原因

Hibernate architecture introduction and environment construction (very detailed)

Wireshark对IMAP抓包分析

Gradle的环境安装与配置

The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high

权限设计=功能权限+数据权限

CSDN add on page Jump and off page specified paragraph jump

excel如何实现中文单词自动翻译成英文?这个公式教你了

18亿像素火星全景超高清NASA放出,非常震撼
随机推荐
mysql集群
CXF
SSL/TLS、对称加密和非对称加密和TLSv1.3
后序线索二叉树
森林的先序和中序遍历
Ad20 learning notes I
二叉排序树
库项目和App项目中清单文件的包名不要相同
发送邮件工具类
先序线索二叉树
iomanip头文件在实战中的作用
平衡二叉树AVL
Today's 61 Fu
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
mysql版本升级+数据迁移
Hbuilderx uses the gaude map to obtain the current location
IDEA中如何生成get/set方法
Record the ideas and precautions for QT to output a small number of pictures to mp4
流数据
MySQL自定义函数实例