【发布时间】:2018-01-16 10:17:01
【问题描述】:
Javascript 构造器 + 创建对象示例
//Constructor
function Course(title,instructor,level,published,views){
this.title = title;
this.instructor = instructor;
this.level = level;
this.published = published;
this.views = views;
this.updateViews = function() {
return ++this.views;
}
}
//Create Objects
var a = new Course("A title", "A instructor", 1, true, 0);
var b = new Course("B title", "B instructor", 1, true, 123456);
//Log out objects properties and methods
console.log(a.title); // "A Title"
console.log(b.updateViews()); // "123457"
什么是 python 等价物? (构造函数/或类+创建对象实例+注销属性和方法?)
python中的self和Javascript中的this有区别吗?
【问题讨论】:
-
差不多,没区别。尽管在 python 中,
self是约定俗成的,你可以随意称呼它。此外,您必须将其作为构造函数的第一个参数包含在内,而 JS “只是本能地知道”this是什么 -
您的问题标题已转换术语
-
哎呀没意识到会交换
标签: javascript python object constructor