JavaScript構造+創建對象,例如的Javascript`this` VS Python的`self`構造
//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相當於此? (構造函數/或類+創建對象實例+註銷屬性&方法?)
有self
從Python和從Javascript this
之間的差?
非常多,沒有區別。儘管在python中,「self」是慣例,你可以任意調用它。另外,你必須把它作爲你的構造函數的第一個參數,而JS只是本能地知道這個是什麼 – inspectorG4dget
你有沒有諮詢[關於類的Python文檔](https://docs.python.org/3 /tutorial/classes.html)? –
您的問題標題已轉換 – Mangohero1