简体   繁体   English

Typescript 无法访问 object 属性

[英]Typescript cant access object property

I have little issue accessing object property in typscript.我在打字稿中访问 object 属性几乎没有问题。 this works fine in js but doesn't in ts这在 js 中可以正常工作,但在 ts 中不行

let getUserName = {
    firstname : "timi",
    lastname: "oluwayomi",
    middlename: "ola",
    full : () => { return this.firstname + this.lastname + this.middlename } ,
    first: () => {  return this.firstname }
  }
console.log(getUserName.first())

in javascript output: timi but ts throws error.在 javascript output: timi 但 ts 抛出错误。 is there a different way to access it in ts?在 ts 中有不同的访问方式吗?

just refer to the same object in the object, this is a hack IMO只需在 object 中引用相同的 object,这是一个 hack IMO

let getUserName = {
    firstname : "timi",
    lastname: "oluwayomi",
    middlename: "ola",
    full : () => { return getUserName.firstname + getUserName.lastname + getUserName.middlename } ,
    first: () => {  return getUserName.firstname }
  }
console.log(getUserName.first())

this inside arrow function refers to the global object, so its giving error, you can try with traditional functions这个内箭头function是指全局object,所以报错,可以用传统函数试试

 let getUserName = {
    firstname : "timi",
    lastname: "oluwayomi",
    middlename: "ola",
    full : function() { return this.firstname + this.lastname + this.middlename } ,
    first: function() {  return this.firstname }
  }
console.log(getUserName.first())

i was working on an component based project "Angular" so i had to initialize first我正在开发一个基于组件的项目“Angular”,所以我必须先初始化

getUserName: any;获取用户名:任意;

getUserName = {
    firstname : "timi",
    lastname: "oluwayomi",
    middlename: "ola",
    full : () => { return getUserName.firstname + getUserName.lastname + getUserName.middlename } ,
    first: () => {  return getUserName.firstname }
  }
console.log(getUserName.first())

also thanks to Dean Van Greunen还要感谢 Dean Van Greunen

You are accessing this within an arrow function.您正在箭头 function 内访问this Inside an arrow function, this refers to globalThis , not the parent object.在箭头 function 内, this指的是globalThis ,而不是父 object。 See You Don't Know JS Yet - Chapter 3 - this Keyword for more info on this .参见你还不知道 JS - 第 3 章 - this关键字this获取更多信息。 So, to start with, use this:所以,首先,使用这个:

let getUserName = {
  firstname : "timi",
  lastname: "oluwayomi",
  middlename: "ola",
  full() { return this.firstname + this.lastname + this.middlename },
  first() {  return this.firstname },
}

As this is of type any in this case, as the TypeScript compiler cannot infer the type of your object for you, you will need to type the object yourself (usually a good idea anyway):由于在这种情况下thisany类型,因为 TypeScript 编译器无法为您推断 object 的类型,您将需要自己键入 object (通常是个好主意)

type GetUserName = {
  firstname: string;
  lastname: string;
  middlename: string;
  full(): string;
  first(): string;
}

let getUserName: GetUserName = {
  firstname: "timi",
  lastname: "oluwayomi",
  middlename: "ola",
  full(this: GetUserName) {
    return this.firstname + this.lastname + this.middlename;
  },
  first(this: GetUserName) {
    return this.firstname;
  },
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM