Javascript のすべての関数にはプロトタイプ属性があり、このプロトタイプ属性はオブジェクト型オブジェクトです。この関数によって構築されたすべてのオブジェクトは、このプロトタイプの特性を持ちます。つまり、構築されたオブジェクトを使用してプロトタイプのプロパティとメソッドに直接アクセスできます。の上。
次のコードは、プロトタイプの使用方法を示しています:
function Staff(name) {
this.name = name;
}
Staff.prototype.say = function() {
alert(this.name "say hello");
}
var Staff1 = new Staff("hunter");
var Staff2 = new Staff("dangjian");
staff1.say();
プロトタイプのもう 1 つの一般的な機能は、プロトタイプを通じてオブジェクトの継承関係を構築することです。基底クラスのオブジェクトをサブクラスのプロトタイプに割り当てることで、オブジェクト指向の継承関係をシミュレートできます。これは、JavaScript のオブジェクト指向メカニズムとよく呼ばれるものです。次のコード スニペットは、この機能を使用してオブジェクトを構築する際の継承関係を示しています。
this.name = name;
}
Staff.prototype.say = function() {
alert(this. name " こんにちは");
}
function ManStaff(name, age) { // サブクラス
this.name = name;
this.age = age; ManStaff.prototype = new Staff(); // 継承関係を確立します
var manStaff("hunter", 22);
var manStaff("dangjian", 32); .say ();
manStaff2.say();
コードを実行すると、ManStaff オブジェクトが基底クラス Staff に Say メソッドを持っていることがわかります。 JavaScriptを実現しました。上記のプロトタイプの使用法はよくご存じかもしれませんが、プログラマとしては、その使用法を知るだけでなく、それがプロトタイプの内部メカニズムであることも理解する必要があります。プロトタイプの原理とプロトタイプチェーンの実装を分析してみましょう。
プロトタイプの仕組みを理解するには、JavaScript で関数がどのように作成されるかを理解する必要があります。
関数 Staff(name) {this.name = name;} に対してコードが実行されると、インタープリタは var Staff = new Function("name", "this.name = name") を実行することと同じになります。事前定義された Function() コンストラクターを使用して、関数型オブジェクト、つまり Staff を作成します。
次に、作成した Staff オブジェクトに __proto__ 属性を追加し、それを Function コンストラクターのプロトタイプに割り当てます。このステップは、var x = new X () メソッドなどを実行するときのすべてのステップです。 X のプロトタイプを :
Staff.__proto__ = Function.prototype;
o.constructor = ベース;
From the above analysis, we can see that when an object is created, a private attribute __proto__ is created, and when a function is created, a prototype attribute is created. Because Staff is a function type object, it will have both properties.
These two properties are key properties for building a prototype chain. Let's analyze how the prototype is passed when executing the code var staff1 = new Staff("hunter").
According to the above analysis, staff1.__proto__ = Staff.prototype, and Staff.prototype is an object created by Object, that is, Staff.prototype.__proto__ = Object.prototype, so staff1.__proto__ .__proto__ points to Object. prototype, that is, staff1.__proto__ .__proto__ == Object.prototype, this is the prototype chain. When you want to read the properties of an object, JS first looks for whether the object itself has this property. If not, it will continue to search along the prototype chain. this property.
If you know the principle of prototype chain, it is easy to build object inheritance in Javascript based on this principle.
From the above analysis, we know that the top of the prototype chain is Object.prototype, which means that Object is the base class of all objects in the built inheritance relationship. You can run the following code verification.
Object.prototype.location = "China";
function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this.name " say hello") ;
}
var ManStaff1 = new Staff("hunter");
var ManStaff2 = new Staff("dangjian");
alert(ManStaff1.location);
alert(ManStaff2. location);
The running results show that Object is the base class of Staff, so how to build a subclass of Staff?
Understanding the establishment principle of the above function, we can easily write the following code:
function Staff(name) { // Base class
this.name = name;
}
Staff.prototype.say = function() {
alert(this .name " say hello");
}
function ManStaff(name, age) { // Subclass
Staff.call(this,name);
this.age = age;
}
ManStaff.prototype = new Staff(); // Establish inheritance relationship
var ManStaff1 = new ManStaff("hunter", 22);
var ManStaff2 = new ManStaff("dangjian", 32) ;
ManStaff1.say();
ManStaff2.say();
This is the sentence that establishes the inheritance relationship: ManStaff.prototype = new Staff(); , the inheritance relationship is calculated as follows :ManStaff1.__proto__ = =ManStaff.prototype, ManStaff.prototype.__proto__ = Staff.prototype, Staff.prototype.__proto__ == Object.prototype; then ManStaff1.__proto__.__proto__.__proto__ == Object.prototype.
This inheritance relationship in JavaScript is looser than the traditional object-oriented inheritance relationship, and the construction method is more difficult to understand, but as a scripting language, its functions are already very powerful.

Javaandjavascriptaredistinctlanguages:javaisusedforenterpriseandmobileapps、whilejavascriptisforinteractivewebpages.1)javaiscompiled、staticatically、andrunsonjvm.2)javascriptisisterted、dynamsornoded.3)

JavaScriptコアデータ型は、ブラウザとnode.jsで一貫していますが、余分なタイプとは異なる方法で処理されます。 1)グローバルオブジェクトはブラウザのウィンドウであり、node.jsのグローバルです2)バイナリデータの処理に使用されるNode.jsの一意のバッファオブジェクト。 3)パフォーマンスと時間の処理にも違いがあり、環境に従ってコードを調整する必要があります。

javascriptusestwotypesofcomments:シングルライン(//)およびマルチライン(//)

PythonとJavaScriptの主な違いは、タイプシステムとアプリケーションシナリオです。 1。Pythonは、科学的コンピューティングとデータ分析に適した動的タイプを使用します。 2。JavaScriptは弱いタイプを採用し、フロントエンドとフルスタックの開発で広く使用されています。この2つは、非同期プログラミングとパフォーマンスの最適化に独自の利点があり、選択する際にプロジェクトの要件に従って決定する必要があります。

PythonまたはJavaScriptを選択するかどうかは、プロジェクトの種類によって異なります。1)データサイエンスおよび自動化タスクのPythonを選択します。 2)フロントエンドとフルスタック開発のためにJavaScriptを選択します。 Pythonは、データ処理と自動化における強力なライブラリに好まれていますが、JavaScriptはWebインタラクションとフルスタック開発の利点に不可欠です。

PythonとJavaScriptにはそれぞれ独自の利点があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1. Pythonは、データサイエンスやバックエンド開発に適した簡潔な構文を備えた学習が簡単ですが、実行速度が遅くなっています。 2。JavaScriptはフロントエンド開発のいたるところにあり、強力な非同期プログラミング機能を備えています。 node.jsはフルスタックの開発に適していますが、構文は複雑でエラーが発生しやすい場合があります。

javascriptisnotbuiltoncorc;それは、解釈されていることを解釈しました。

JavaScriptは、フロントエンドおよびバックエンド開発に使用できます。フロントエンドは、DOM操作を介してユーザーエクスペリエンスを強化し、バックエンドはnode.jsを介してサーバータスクを処理することを処理します。 1.フロントエンドの例:Webページテキストのコンテンツを変更します。 2。バックエンドの例:node.jsサーバーを作成します。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ドリームウィーバー CS6
ビジュアル Web 開発ツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター
