我想了解javascript中的原型.例如:
1)
var MyObject = function() {
this.a = "a";
return this;
}
MyObject.prototype.fn = function() {console.log('1');}
var obj1 = new MyObject1();
Run Code Online (Sandbox Code Playgroud)
2)
var MyObject = function() {
this.a = "a";
this.fn = function() {console.log('1');}
return this;
}
var obj2 = new Object2();
Run Code Online (Sandbox Code Playgroud)
我得到obj1和obj2一样.所以,我在2)做的也是原型?或不?我对原型缺少什么?
我在Javascript中看到了这两种声明方法的方法:
var User = function() {
this.name = 'Foo';
this.greet = function() {
console.log('Hello!');
}
}
Run Code Online (Sandbox Code Playgroud)
和
var User = function() {
this.name = 'Foo';
}
User.prototype.greet = function() {
console.log('Hello!');
}
Run Code Online (Sandbox Code Playgroud)
有什么区别?
我正在使用Lightbox2
https://github.com/lokesh/lightbox2/blob/master/js/lightbox.js
我不明白为什么Lightbox的所有内部成员都是prototyped(Lightbox.prototype.init)而不仅仅是members(Lightbox.init)?
如果它们特定于每个灯箱实例会不会更容易使用this.init?
我完全理解为什么它是更好地使用原型,而不是构造函数的定义一个类的方法(即对"这个"在JavaScript"原型"的用途?)不过,我最近碰到一个前来HashMap类定义count 属性的原型和map构造函数中的属性:
js_cols.HashMap = function(opt_map, var_args) {
/**
* Underlying JS object used to implement the map.
* @type {!Object}
* @private
*/
this.map_ = {};
/...
}
/**
* The number of key value pairs in the map.
* @private
* @type {number}
*/
js_cols.HashMap.prototype.count_ = 0;
Run Code Online (Sandbox Code Playgroud)
是否有优势count在原型中声明实例属性而不是this.count_ = 0;在构造函数中说?如果是这样,为什么不js_cols.HashMap.prototype.map_ = {};呢?
编辑:一个类似的问题被问到,为什么在JavaScript中为实例变量的原型声明属性,并且"默认值"被引发作为用例,但是没有解释为什么这比仅仅定义默认值更合乎需要.构造函数.
有没有理由编写ES6方法的经典语法?
class MyClass {
myMethod() {
this.myVariable++;
}
}
Run Code Online (Sandbox Code Playgroud)
当我myMethod()在某些事件上使用回调时,我必须写这样的东西(在JSX中):
// Anonymous function.
onClick={() => { this.myMethod(); }}
// Or bind this.
onClick={this.myMethod.bind(this)}
Run Code Online (Sandbox Code Playgroud)
但是如果我将方法声明为箭头函数:
class MyClass {
myMethod = () => {
this.myVariable++;
}
}
Run Code Online (Sandbox Code Playgroud)
我只能写(在JSX中):
onClick={this.myMethod}
Run Code Online (Sandbox Code Playgroud) 我对这两种向函数添加方法的方式感到困惑.让我举个例子来解释一下.
var foo = function(){
this.bar = function(){alert('I am a method')}
}
foo.prototype.baz = function(){alert('I am another method')}
var car = new foo();
现在,在这一点上我们可以使用汽车的baz和bar方法.好吧,但它们之间有什么区别.为函数原型或它的构造函数添加方法的细微差别是什么.
谢谢..
假设我有一个类和一些静态辅助方法,如下所示:
function MyClass (myVar) {
this.myVar = myVar;
this.replaceMe = function (value) {
// this will fail
this = MyClass.staticHelper( value );
return this;
}
this.revealVar = function () {
alert( this.myVar );
}
}
MyClass.staticHelper = function (instance, value) {
return new MyClass( instance.myVar + value );
}
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的:
var instance = new MyClass( 2 );
instance.revealVar(); // alerts 2
instance.replaceMe( 40 ).revealVar(); // alerts 42
Run Code Online (Sandbox Code Playgroud)
原因是我的类有一个稍微复杂的结构,我不想每次手动分配所有内部变量,而是替换整个对象.有一个简单的方法吗?
之间有什么区别:
function Gandalf() {
this.color = 'grey';
}
Gandalf.prototype.comeBack = function() {
this.color = 'white';
}
Run Code Online (Sandbox Code Playgroud)
和:
function Gandalf() {
this.color = 'grey';
this.comeBack = function() {
this.color = 'white';
return this;
};
}
Run Code Online (Sandbox Code Playgroud)
在什么情况下我应该将该方法挂接到原型上,或者不挂接?
在codecademy.com上浏览javascript课程时,我有点困惑.
首先,我们一直在学习如何向类添加方法:
function Dog (breed) {
this.breed = breed;
this.sayHello = function () {
console.log("Hello this is a " + this.breed + " dog");
}
};
var someDog = new Dog("golden retriever");
someDog.sayHello();
Run Code Online (Sandbox Code Playgroud)
然后我们开始了"原型".还有这个例子:
function Dog (breed) {
this.breed = breed;
};
Dog.prototype.sayHello = function () {
console.log("Hello this is a " + this.breed + " dog");
}
var someDog = new Dog("golden retriever");
someDog.sayHello();
Run Code Online (Sandbox Code Playgroud)
两个例子都给出了相同的结果.这两个例子只是两种做同样事情的方式吗?或者两者之间存在实际差异?
function Candy(name) {
this.name = name;
}
Candy.prototype.printName = function () {
console.log(this.name);
}
var chocolate = new Candy("chocolate");
chocolate.printName();
var gummyBears = new Candy("gummy bears");
gummyBears.printName();
Run Code Online (Sandbox Code Playgroud)
这与不使用原型完全相同:
function Candy(name) {
this.name = name;
this.printName = function () {
console.log(this.name);
}
}
var chocolate = new Candy("chocolate");
chocolate.printName();
var gummyBears = new Candy("gummy bears");
gummyBears.printName();
Run Code Online (Sandbox Code Playgroud)
所以我不确定使用原型的优势是什么!