你能解释一下构造函数中的设置方法和原型对象之间的区别吗?下面的代码显示设置方法的这两种方式- say_hello而且say_bye都很好地工作:
function MessageClass() {
this.say_bye = function() { alert('see ya'); };
}
MessageClass.prototype.say_hello = function() { alert('hello'); };
x = new MessageClass();
x.say_hello();
x.say_bye();
Run Code Online (Sandbox Code Playgroud) 将方法"area"定义为"this"的属性而不是"prototype"的区别是什么?
//console.clear()
function Rectangle(w, h)
{
this.width = w;
this.height = h;
this.area = function( ) { return this.width * this.height; }
}
var r = new Rectangle(2, 3);
var a = r.area( );
//console.log(a)
function Square(s)
{
this.side= s;
}
Square.prototype.area = function(){return this.side * this.side; }
var r = new Square(2);
var a = r.area( );
//console.log(a)
Run Code Online (Sandbox Code Playgroud)
在JavaScript - The definitive guide本节Prototypes and Inheritance中Chapter 9 , part 1,作者说在原型对象中定义方法"area"是有益的,但他的解释并不是真的可以理解:
"..每个Rectangle对象的区域总是指同一个函数(当然,有人可能会改变它,但你通常打算让对象的方法保持不变).对于那些方法使用常规属性是低效的.意图由同一个类的所有对象共享(即,使用相同构造函数创建的所有对象)."
我知道这个问题看起来几乎像这样一个,但事实并非如此.
我去了各种网站,但无法理解以下向自定义对象添加方法的方法之间的区别:
方法1:
function circle(radius){
this.radius = radius;
this.area = function(){ return 3.14*this.radius*this.radius;}
}
Run Code Online (Sandbox Code Playgroud)
方法2:
function circle(radius){
this.radius = radius;
}
circle.prototype.area = function(){ return 3.14*this.radius*this.radius; }
Run Code Online (Sandbox Code Playgroud)
是否存在其中一种方法存在的性能或设计问题,另一种方法没有?
这是代码吗
function Person() {
function myMethod() {
alert ('hello');
}
this.method = myMethod;
}
Run Code Online (Sandbox Code Playgroud)
相当于:
function Person() { }
Person.prototype.method2 = function() {
alert ('hello');
};
Run Code Online (Sandbox Code Playgroud)
如果是,我应该使用哪种方法定义?为什么?
今天我与同事讨论了Javascript中的嵌套函数:
function a() {
function b() {
alert('boo')
}
var c = 'Bound to local call object.'
d = 'Bound to global object.'
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,试验指出b在a的体外是不可达的,就像c一样.但是,d是 - 执行a()之后.在ECMAScript v.3标准中寻找这种行为的确切定义,我没有找到我正在寻找的确切措辞; 第13节第71页没有说明,是函数声明语句创建的函数对象要绑定到哪个对象.我错过了什么吗?
在Javascript中,这两种向对象添加函数的方法有什么区别吗?出于任何原因,一个人更好吗?
function ObjA() {
this.AlertA = function() { alert("A"); };
}
ObjA.prototype.AlertB = function() { alert("B"); };
var A = new ObjA();
A.AlertA();
A.AlertB();
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
var MyClass = function(b) {
this.a = b;
this.getA = function() {
return that.a;
}
}
var SecondClass = function(b) {
this.prototype = new MyClass(b);
this.getB = function() {
return 6;
}
}
var a = new SecondClass(2);
console.log(a.getA());
Run Code Online (Sandbox Code Playgroud)
输出告诉我a没有名为getA()的方法
我假设在SecondClass的构造函数中执行this.prototype = new MyClass()会导致它从MyClass的inhert方法?
我确信有更好的方法可以做到这一点,但我试图理解prototype关键字的行为.
var p = function () {
this.show = function () {
alert('hello world!!!');
}
}
p.prototype.show = function() {
alert('haha');
}
var o = new p();
o.show();
Run Code Online (Sandbox Code Playgroud)
它警告"hello world!!!",为什么?
我可以修改原型方法,如果是的话怎么样?
有什么不同?有没有?
var Likes = function (el) {
this.el = $(el);
return this;
};
Likes.prototype.add = function (name) {
this.el.find('.no-results').remove();
$('<li>', { text: name }).appendTo(this.el);
};
Run Code Online (Sandbox Code Playgroud)
和:
var Likes = function (el) {
this.el = $(el);
this.add = function (name) {
this.el.find('.no-results').remove();
$('<li>', { text: name }).appendTo(this.el);
};
return this;
};
Run Code Online (Sandbox Code Playgroud) 我想了解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 ×10
prototype ×5
constructor ×1
ecma262 ×1
inheritance ×1
methods ×1
oop ×1
scope ×1