Comments (4)
Hi @gjssss ,
报错是因为启用了 strictNullChecks
编译选项。如果关闭该选项,代码就可以运行了。
但根本原因是这段代码确实有 bug,因为它直接在 first
上调用了 first.hasOwnProperty(prop)
,但 first
上不一定有该方法。例如 first = Object.create({}, null)
。
extend
函数将两个参数的自身可枚举属性复制到 result
上。您说的无法获取 log
方法指的是哪里?
from typescript.
Hi @gjssss ,
报错是因为启用了
strictNullChecks
编译选项。如果关闭该选项,代码就可以运行了。但根本原因是这段代码确实有 bug,因为它直接在
first
上调用了first.hasOwnProperty(prop)
,但first
上不一定有该方法。例如first = Object.create({}, null)
。
extend
函数将两个参数的自身可枚举属性复制到result
上。您说的无法获取log
方法指的是哪里?
您好,谢谢您的回答,我关闭了strictNullChecks
的编译选项,我这边说的log
方法指的是最后一句代码jim.log(jim.name);
而当我试图显示其log
属性时显示undefined
为什么会出现这种情况呀,看样子好像是函数extend
中second
对象没有log
属性,我试着打印second
的内容发现显示出[LOG]: ConsoleLogger: {}
是一个空对象,不清楚出了什么状况。
下面是我运行的完整代码,不知道是不是因为我代码出了错。
function extend<First, Second>(first: First, second: Second): First & Second {
const result: Partial<First & Second> = {};
for (const prop in first) {
if (first.hasOwnProperty(prop)) {
(result as First)[prop] = first[prop];
}
}
for (const prop in second) {
if (second.hasOwnProperty(prop)) {
(result as Second)[prop] = second[prop];
}
}
// 显示second时是空对象
console.log(second);
return result as First & Second;
}
class Person {
constructor(public name: string) { }
}
interface Loggable {
log(name: string): void;
}
class ConsoleLogger implements Loggable {
log(name: string) {
console.log(`Hello, I'm ${name}.`);
}
}
const jim = extend(new Person('Jim'), ConsoleLogger.prototype);
// 运行错误
jim.log(jim.name);
// console.log(jim); // jim对象只有name属性
最后谢谢您的回答和热心帮助!
from typescript.
@gjssss Aha! 我在重现的时候顺手设置了 target: es5
所以没问题,Playground 默认应该是 ESNext
。手册里写了,可能没注意:下面是如何创建混入的一个简单例子("target": "es5"):
。
在目标为 ES5
时,TS 会将 class
转换成 function
,而 ESNext
则直接使用 class
。这两者对可枚举属性的定义不同。ES5
下 log
是可枚举属性,内部属性 [[Enumerable]]: true
。而 ES6
以后,ConsoleLogger.prototype
上的 log
是不可枚举属性,内部属性 [[Enumerable]]: false
。
from typescript.
@zhongsp 啊!谢谢!我可能没搞清楚("target": "es5")
是什么意思,谢谢!麻烦啦!
from typescript.
Related Issues (20)
- 总算找到一个有每个版本更新内容汉化的地方了 HOT 1
- TypeScript 4.4 Release Notes
- TypeScript 4.5 Release Notes HOT 3
- 有个问题请教下 HOT 3
- 目录跟官网的完全不一样 HOT 2
- Future of This Project HOT 3
- 关于在“高级类型”一章中“分布式有条件类型”这一小节中最后一个例子的问题 HOT 1
- 建议发布PDF格式的汇总, MD在不同的文件夹下有点蛋疼 HOT 1
- TypeScript 4.6 Release Notes HOT 1
- 文件名称中有空格的无法打开 HOT 1
- TypeScript 4.7 release notes HOT 1
- “联合类型和交叉类型”这一章还计划翻译吗 HOT 2
- 翻译错误 HOT 1
- TypeScript 4.8 release notes HOT 1
- 手册(进阶) 高级类型 章节,标点错误 HOT 1
- 【赠书活动】首位提交 TypeScript 4.9 发布说明翻译的小伙伴将得到本人所作的《TypeScript入门与实战》一本!
- 關於 https://typescript.bootcss.com/ 網站內容錯誤。 HOT 1
- 接口-可索引的类型 HOT 1
- 官网为啥只翻译了部分呢 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.