JavaScriptのMapとWeakMapの違いは何ですか?
MapとWeakMapの違い
MapとWeakMapの機能メカニズムは同じですが、ほとんど違いはありません。
1)ウィークマップ マップに対して、キーとしてオブジェクトのみを受け入れます 、オブジェクトに加えて、文字列、数値などのプリミティブデータ型を受け入れます。
2) WeakMap キーのように機能しているオブジェクトへの参照がない場合、オブジェクトはガベージコレクションを回避しません。したがって、 WeakMapでキーを取得する方法はありません。 、一方、マップ キーを取得するためのMap.prototype.keys()などのメソッドがあります。
3) WeakMapにサイズプロパティが存在しません 。
地図
文字列、数値、オブジェクトなどのデータ型に関係なく、キーを値に関連付けるために使用されます。
例
<html>
<body>
<script>
// Creates a new Map object
var map = new Map();
// Defines an object that will be used a key in the ma
var objKey = {name: 'tutorialspoint'};
document.write("</br>");
// Adds a new element having a String as its key and a String as its value
map.set('first', 'a');
document.write("</br>");
// Adds a new element having a Number as its key and an Array as its value
map.set(3, ['c']);
document.write("</br>");
// Adds a new element having an Object as its key and a Number as its value
map.set(objKey, 3);
// Adds a new element having an Array as its key and a String as its value
map.set(['add', 'mapping'], 'd');
// Checks whether an element having a key of "2" exists in the map.
document.write(map.has(2));
document.write("</br>");
// Checks whether an element having a key of "first" exists in the map.
document.write(map.has('first'));
document.write("</br>");
// Retrieves the element having key of "first". Prints "a"
document.write(map.get('first'));
document.write("</br>");
// Retrieves the element having as a key the value of objKey.
document.write(map.get(objKey));
document.write("</br>");
// Retrieves the element having key of "empty". Prints "undefined"
document.write(map.get('empty'));
document.write("</br>");
// Retrieves the map size. Prints "4"
document.write(map.size);
document.write("</br>");
// deletes all the value
map.clear();
document.write(map.size);
</script>
</body>
</html> 出力
false true a 3 undefined 4 0
WeakMap
以下の例では、 WeakMapを見つけることができます オブジェクトのみを受け入れ、プリミティブ値(文字列、数値)は受け入れません
例
<html>
<body>
<script>
// Creates a new WeakMap object
var weakMap = new WeakMap();
// Defines an object that will be used a key in the map
var obj4 = {d: 4};
// Defines another object that will be used a key in the map
var obj5 = {e: 5};
// Adds a new element having an Object as its key and a String as its value
weakMap.set(obj4, 'fourth');
// Adds a new element having an Object as its key and a String as its value
weakMap.set(obj5, 'fifth');
// Adds a new element having a Function as its key and a Number as its value
weakMap.set(function(){}, 7);
// Checks whether an element having as its key the value of objKey4 exists in the weak map.
document.write(weakMap.has(obj4));
document.write("</br>");
// Retrieve the value of element associated with the key having the value of objKey4. Prints "first"
document.write(weakMap.get(obj4));
document.write("</br>");
// Deletes the element having key of objKey4. Prints "true"
document.write(weakMap.delete(obj4));
document.write("</br>");
// Deletes all the elements of the weak map
weakMap.clear();
</script>
</body>
</html> 出力
true fourth true
-
JavaScriptの関数とメソッドの違いは何ですか?
JavaScriptでも関数とメソッドは同じですが、メソッドはオブジェクトのプロパティである関数です。 以下はJavaScriptの関数の例です- function functionname(param1, param2){ // code } 例 メソッドはオブジェクトに関連付けられた関数です。以下はJavaScriptのメソッドの例です- <html> <head> <script> var e
-
JavaとJavaScriptの違いは何ですか?
Web開発者のJeremyKeithが2009年に述べたように、「JavaはJavaScriptに、ハムはハムスターになります」。そのアナロジーの正確さは議論の余地がありますが、その背後にある精神は確かです。JavaとJavaScriptは、共通の言語ルートを共有しているにもかかわらず、2つの非常に異なるプログラミング言語です。何年にもわたって、それらはもう少し重複するようになりましたが、JavaScriptは、Webサイトをインタラクティブにする主要なフロントエンド言語であり続け、Javaはサーバーサイドおよびアプリケーションプログラミングで引き続き人気があります。開発者を雇ったり、コーディ