简体   繁体   English

Javascript - 如何在方法中获取/设置? (例如 pineapple.is_a.fruit)

[英]Javascript - How do I have a get/set in a method? (e.g. pineapple.is_a.fruit)

I have an assignment where I'm supposed to make magic out of programming.我有一个任务,我应该从编程中变魔术。 I'm unable to find any answers online, as I do not know the search term for it (tried method in a method etc...).我无法在网上找到任何答案,因为我不知道它的搜索词(在方法中尝试过方法等......)。 Appreciate any help given!感谢您提供的任何帮助!

Here's what I got: I need to create a class that builds upon itself.这是我得到的:我需要创建一个基于自身构建的类。 eg例如

const pineapple = new Item('pineapple');
pineapple.type = fruit // this is simple

pineapple.is_a.fruit = true // this I do not know
pineapple.is.heavy = true // same thing

I do not even know where to begin.我什至不知道从哪里开始。 My attempt is similar to this, but I'm getting undefined.我的尝试与此类似,但我变得不确定。

class Thing {
  constructor(type) {
    this.type = type;
  }
  
  is_a(bool) {
    this.fruit = this.is_a(bool);
  }
}

Assuming that they can be defined in advance, in order to have sub-properties like pineapple.is_a.fruit , you'll need to define objects on the object's is_a and is properties.假设它们可以提前定义,为了拥有像pineapple.is_a.fruit这样的子属性,您需要在对象的is_ais属性上定义对象。 For instance (see comments):例如(见评论):

 class Item { // `Item` rather than `Thing`, right? constructor(type) { this.type = type; // Create an `is_a` property that's an object with a `fruit` property this.is_a = { fruit: false // Or whatever the initial value should be }; // Create an `is` property that's an object with a `heavy` property this.is = { heavy: false // Or whatever the initial value should be }; } } const pineapple = new Item('pineapple'); pineapple.type = "fruit"; // I added quotes here console.log("is_a.fruit before:", pineapple.is_a.fruit); console.log("is.heavy before:", pineapple.is_a.fruit); pineapple.is_a.fruit = true; pineapple.is.heavy = true; console.log("is_a.fruit after: ", pineapple.is_a.fruit); console.log("is.heavy after: ", pineapple.is_a.fruit);

暂无
暂无

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

相关问题 Java中是否可能存在竞争条件? (例如:我想自动获取并设置值) - Is race condition possible in Javascript? (e.g.: I want to get and set value atomically) 如何在Javascript中打印罗马语(例如西班牙语)/特殊字符? - How do I print roman languages (e.g. Spanish) /special characters in Javascript? 当javascript创建对象(例如img标签)时,它没有结束标记。 如何使W3C有效? - When objects (e.g. img tag) is created by javascript, it doesn't have the closing tag. How do I make it W3C valid? 在JavaScript中,如何从像素值(例如2em或20px)获取单位(例如em或px)? - In javascript, how to get the unit (e.g. em or px) from a pixel value (e.g. 2em or 20px)? 如何使用 jQuery/普通旧 javascript 更改元素(例如 h1 -> h2)? - How do I change an element (e.g. h1 -> h2) using jQuery / plain old javascript? 在Javascript中,如何解码字符串中包含二进制(例如非UTF-8)数据的字符串? - In Javascript, how do I decode a string in which decoded string contains binary (e.g. non UTF-8) data? 如何使用 kotlin JS 测试 UI? (例如在页面上有一个元素) - How can I test UI with kotlin JS? (e.g. have an element on a page) 当用户输入特殊字符时如何显示菜单或下拉列表,例如 % - How can I have display a menu or dropdown list when user enters a special character e.g. % 单击时如何使用Javascript(而非jQuery)获取元素的属性(例如标题)? - How to get the attribute (e.g., title) of an element when clicked on, with Javascript (not jQuery)? 我如何限制动画,例如根据捏合手势比例设置它们 - how can i throttle animations, e.g. set them based on pinch gesture scale
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM