Javascriptのコンストラクタ+オブジェクトの例を作成Javascriptを `this` 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の同等は何ですか? (コンストラクタ関数/またはクラス+プロパティ&メソッドをログアウト+オブジェクトのインスタンスを作成します?)
はJavaScriptからのpythonからself
とthis
の間に違いはありますか?ここで
かなり、違いはありません。 Pythonでは、 'self'は慣習であり、あなたはそれをあなたが望むものと呼ぶことができます。また、JSをコンストラクタの最初のパラメータとして含める必要がありますが、JSは "本能的に"これが何であるかを知っています – inspectorG4dget
[クラスに関するPythonのドキュメント](https://docs.python.org/3 /tutorial/classes.html)? –
あなたの質問には、用語の切り替えがあります – Mangohero1