作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的脚本中创建一个类。
Google Apps Script 语言基于 javaScript,所以我从 javaScript 手册中举了一个例子:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Missing ; before statement. (line 1, file "Code")
最佳答案
更新:
自 2020 年 Spring 起,Google 推出了 new runtime适用于支持 Classes 的 Apps 脚本.
新脚本默认使用此运行时,而旧脚本必须转换。脚本编辑器中会显示一个提示,您可以从中转换脚本。
// V8 runtime
class Rectangle {
constructor(width, height) { // class constructor
this.width = width;
this.height = height;
}
logToConsole() { // class method
console.log(`Rectangle(width=${this.width}, height=${this.height})`);
}
}
const r = new Rectangle(10, 20);
r.logToConsole(); // Outputs Rectangle(width=10, height=20)
原始(旧)答案:
var Polygon = function(height, width){
this.height = height;
this.width = width;
this.logDimension = function(){
Logger.log(this.height);
Logger.log(this.width);
}
};
function testPoly(){
var poly1 = new Polygon(1,2);
var poly2 = new Polygon(3,4);
Logger.log(poly1);
Logger.log(poly2);
poly2.logDimension();
}
关于javascript - 如何在谷歌脚本中声明一个自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33174024/
我是一名优秀的程序员,十分优秀!