在Javascript中,我可以像这样创建一个"类":
var MyClass = function(){
return this;
};
var myClassInstance = new MyClass();
Run Code Online (Sandbox Code Playgroud)
执行上述操作和执行此操作之间有什么区别:
var MyClass = {};
var myClassInstance = Object.create(MyClass);
Run Code Online (Sandbox Code Playgroud)
有没有理由使用一个而不是另一个?
这个问题与使用"Object.create"而不是"new"不重复.有问题的线程并不专注于在使用时正确传递参数Object.create
我很好奇我将如何使用Object.create而不是初始化对象new.到目前为止,这是我的代码:
function Human(eyes) {
this.eyes = eyes || false;
}
Human.prototype.hasEyes = function() {
return this.eyes;
}
function Male(name) {
this.name = name || "No name";
}
Male.prototype = new Human(true); //passing true to the Human constructor
var Sethen = new Male("Sethen");
console.log(Sethen.hasEyes());
Run Code Online (Sandbox Code Playgroud)
如上所示,Male.prototype = new Human(true);使用true创建一个新对象.hasEyes()运行该函数时,会按预期记录true.
所以,我的问题是..使用Object.create我将如何以同样的方式传递true参数?
更新
如果这是不可能的,请随时提供解释原因的答案。我很乐意将其标记为已接受。
我想稍微简化以下代码(对象“声明”的两个步骤,我想要一个):
var Masher = function(opts) {
this._name = opts.name;
};
Masher.prototype = Object.create(Object.prototype, {
_name: { writable: true },
name: { get: function() { return this._name; }}
});
// Note: (new Masher({name: 'bar'})).name == 'bar'
Run Code Online (Sandbox Code Playgroud)
我想一次性创建整个函数原型,构造函数出现在 Object.create 的某处。也许,像这样:
var Basher = Object.create(Function.prototype, {
_name: { writable: true },
name: { get: function() { return this._name; }},
constructor: { value: function(opts) { this._name = opts.name; }}
});
Run Code Online (Sandbox Code Playgroud)
但是,当我调用时new Basher(),我得到:'TypeError: object is not a function'。
虽然我意识到我可以用语法糖(一个帮助程序库)来做到这一点,但我的目标是让事情尽可能简单,并对 JS 对象、原型、构造函数内部结构有一些了解。我试图阅读尽可能多的内容: …
注意:这不是关于经典和原型继承的问题.它是关于用于初始化对象的编码模式.类构造函数创建并初始化对象,而避免new运算符和Object.create()仅用于创建对象并设置原型链.我还没有找到一个在线资源,它解释了在进行Crockford Object.create()方法时进行创建和初始化的最佳实践编码模式.
如果我有一个构造函数(在我的头脑中,这使我的课,虽然我知道类在主流JavaScript中技术上尚不存在)
function Person(first, last) {
this.name = {
first: first,
last: last
};
}
Person.prototype.tellName = function() {
return this.name.first + ' ' + this.name.last;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以像这样实例化它
var p1 = new Person('John', 'Doe');
var p2 = new Person('Sven', 'Svensson');
Run Code Online (Sandbox Code Playgroud)
并更改Person.name.first,并Person.name.last分别
p1.tellName(); // Output: 'John Doe'
p2.tellName(); // Output: 'Sven Svensson'
p1.name.first = 'Peter';
p2.name.last = 'Celery';
Run Code Online (Sandbox Code Playgroud)
并Person.tellName()使用以下输出执行对象的功能
p1.tellName(); // Output: 'Peter Doe'
p2.tellName(); // Output: 'Sven Celery' …Run Code Online (Sandbox Code Playgroud) 使用或不使用new关键字调用javascript函数之间有区别吗?例如,如果我有这个功能:
function computer(){
this.hardDrive = "big";
this.processor = "fast";
}
Run Code Online (Sandbox Code Playgroud)
然后我以两种不同的方式称呼它:
var hp = computer();
var hp = new computer();
Run Code Online (Sandbox Code Playgroud)
什么是两个函数调用之间的区别?
我正在使用原型继承在javascript中构建面向对象的库.与Java和.NET类似,我的所有对象/原型都将继承"Object"对象/原型.我想知道是否可以从派生的函数调用超级对象/原型函数?
请考虑以下代码示例:
function Object() {
this.DoAction = function() {
};
};
function CustomObject() {
this.DoAction = function() {
super.DoAction(); //How do I do this in JavaScript?
};
};
Run Code Online (Sandbox Code Playgroud) 我一直在阅读使用Javascript的面向对象编程的MDN指南,其中在一个示例中显示继承,提到以下内容:
// Create a Student.prototype object that inherits from Person.prototype.
// Note: A common error here is to use "new Person()" to create the
// Student.prototype. That's incorrect for several reasons, not least
// that we don't have anything to give Person for the "firstName"
// argument. The correct place to call Person is above, where we call
// it from Student.
Student.prototype = Object.create(Person.prototype); // See note below
Run Code Online (Sandbox Code Playgroud)
我也研究了其他答案,但他们没有具体说明使用时会遇到什么问题 Student.prototype = new Person()
上面提到的一个问题涉及传球firstName,但这只是一个场景.
在任何人将此标记为重复之前.这里的问题使用"Object.create"而不是"new"处理通常使用 …
Person.prototype和之间有什么区别Object.create(Person.prototype)?我可以使用它们吗?
function Person(name) {
this.name = name;
}
Person.prototype.copy = function() {
return new this.constructor(this.name);
};
// define the Student class
function Student(name) {
Person.call(this, name);
}
// inherit Person
Student.prototype = Person.prototype;
//Student.prototype = Object.create(Person.prototype);
Run Code Online (Sandbox Code Playgroud)