Leetcode如何在Javascript中创建和使用对象?

时间:2019-01-06 06:48:58

标签: javascript oop object inheritance

我正在尝试解决Leetcode问题并了解Javascript对象。这是问题。

/**
 * Initialize your data structure here.
 */
var MyHashSet = function() {

};

/** 
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.add = function(key) {

};

/** 
 * @param {number} key
 * @return {void}
 */
MyHashSet.prototype.remove = function(key) {

};

/**
 * Returns true if this set contains the specified element 
 * @param {number} key
 * @return {boolean}
 */
MyHashSet.prototype.contains = function(key) {

};

/** 
 * Your MyHashSet object will be instantiated and called as such:
 * var obj = Object.create(MyHashSet).createNew()
 * obj.add(key)
 * obj.remove(key)
 * var param_3 = obj.contains(key)
 */

我对这句话很困惑:

var obj = Object.create(MyHashSet).createNew()

应该不是Object.create(MyHashSet.prototype)吗?

createNew()也是哪里来的?

我猜Leetcode在幕后做一些我不了解的魔术。 感谢您的帮助!

0 个答案:

没有答案