使用构造函数创建的这两个对象到底有什么不同?ab
该属性__proto__对于使用闭包无法实现的任何内容都有用吗?我应该在不同情况下使用这些技术吗?内存使用量有差异吗?
(jsFiddle)
window.MODULE = {};
MODULE.constructor = function(){
this.publicVariable = 10;
};
MODULE.constructor.prototype.publicMethod = function(){
return this.publicVariable;
};
//-------------------------------//
MODULE.closure = (function(){
var publicMethod = function(){
return this.publicVariable;
};
return function(){
var obj = {};
obj.publicVariable = 10;
obj.publicMethod = publicMethod;
return obj;
};
}());
//-------------------------------//
var a = new MODULE.constructor();
console.log("constructor", a.publicMethod(), a)
var b = MODULE.closure();
console.log("closure", b.publicMethod(), b)
Run Code Online (Sandbox Code Playgroud)
还可以看到一个更复杂的jsFiddle与一些私有和静态属性的比较 - 两种技术的工作方式相同,据我所知...
我看看Addy Osmani关于构造函数模式的章节:http://addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript 我发现了以下内容:
function Car( model, year, miles ) {
this.model = model;
this.year = year;
this.miles = miles;
this.toString = function () {
return this.model + " has done " + this.miles + " miles";
};
}
// Usage:
// We can create new instances of the car
var civic = new Car( "Honda Civic", 2009, 20000 );
var mondeo = new Car( "Ford Mondeo", 2010, 5000 );
// and then open our browser console to …Run Code Online (Sandbox Code Playgroud) 我在JavaScript的Revealing Prototype Pattern中遇到私有变量的问题.我无法弄清楚如何在共享(单例)原型中使用多个不同函数中使用的私有变量,而不暴露它们.这是我在JSFiddle中的意思的例子.
问题在于使用var v与this.v.第一个是混淆所有实例的状态,但第二个是公开可见的.有没有办法让v private,并为每个单独的实例保留其状态?
嘿家伙们希望我不会因犯明显错误而受到抨击.我正在研究Brad Dayley的Node.js,MongoDB和Angularjs一书,我坚持他的一个练习(清单4.4).我有一个简单的脚本emitterListener.js,如下脚本旨在对帐户进行检查.
var events = require('events');
function Account() {
this.balance = 0;
events.EventEmitter.call(this);
this.deposit = function(amount) {
this.balance += amount;
this.emit('balanceChanged');
};
this.withdraw = function(amount) {
this.balance -= amount;
this.emit('balanceChanged');
};
}
Account.prototype._proto_ = events.EventEmitter.prototype;
function displayBalance() {
console.log("Account balance: $%d", this.balance);
}
function checkOverdraw() {
if (this.balance < 0) {
console.log("Account Overdrawn!!!!!");
}
}
function checkGoal(acc, goal) {
if (acc.balance > goal) {
console.log("Goal Achieved!!!!!");
}
}
var account = new Account();
account.on("balanceChanged", displayBalance);
account.on("balanceChanged", checkOverdraw);
account.on("balanceChanged", function() { …Run Code Online (Sandbox Code Playgroud) 我在Web应用程序中看到了这段代码,想更好地理解它(以便自己创建这种结构),但是我不确定这是哪种JavaScript。
我的意思是,Class.create()语法是“开箱即用” JavaScript的一部分,还是该Web应用程序中嵌入的某些库的一部分?

以下不起作用,从我的getter中,我看不到_nickname'class'中定义的Person.
var Person = function (args) {
var _nickname = '';
if (args === undefined || args === null) {
return;
}
if (args.nickname !== undefined && args.nickname !== null) {
_nickname = args.nickname;
}
}
Object.defineProperty(Person.prototype, "nickname", {
get : function () {
return _nickname;
}
});
var x = new Person({
nickname : 'bob'
});
console.log(x.nickname);
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标呢?有没有办法在函数中将_nickname添加到Person的原型中?
我是Javascript的新手,我刚刚在codeAcademy完成了很长的Javascript课程.我有一些问题prototype.据我所知,prototype它主要用于继承,也可以动态定义对象的方法.
但我还是有一些问题.看看我的代码.我使用原型toString在对象Animal和另一个中定义了一个toString.当我运行它时,它显示为什么:[Object] Dumbo 4而不是[Proto] Dumbo 4?
function Animal(name, numLegs){
this.name = name;
this.numLegs = numLegs;
this.toString = function(){
return "[Object]" + this.name + " " + this.numLegs + "\n";
};
}
Animal.prototype.toString = function(){
return "[Proto]" + this.name + " " + this.numLegs + "\n";
};
var animal = new Animal("Dumbo", 4);
console.log(animal.toString());
Run Code Online (Sandbox Code Playgroud) 我正在学习Javascript中的原型继承,并且据我所知,我正在尝试使用它将进程发送到无限递归链接.
我对原型继承的想法是一个对象(它是一个函数)持有原型链接.该对象的任何实例都指向它.所以如果我说instance.someproperty它会查看父对象的原型链.
假设这个,如果我只是将函数原型列表指向自身,当对象试图访问某些属性时,它应该进入无限循环.
var p = function(){};
p.prototype = p;
// I also tried p.prototype = new p();
var q = new p();
// and now when I say q.something, it should never be able to find it.
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这不起作用,我怎么能让它进入无限循环.
目前,我在我的JavaScript库中实现了继承,如下所示:
parent = function() { };
parent.prototype.hello = function() { alert('parent'); };
child = function() { };
child.prototype = parent.prototype;
Run Code Online (Sandbox Code Playgroud)
但是,我注意到当我覆盖子类"class"原型中的函数时,它也会不合理地覆盖父类的原型:
child.prototype.hello = function() { alert('child'); }
(new parent()).hello(); // alerts "child" --> undesirable
(new child()).hello(); // alerts "child" --> desirable
Run Code Online (Sandbox Code Playgroud)
如果我从孩子的原型中删除了一些东西
delete child.prototype.hello;
Run Code Online (Sandbox Code Playgroud)
然后父母的原型受到不利影响.也许吧
child.prototype = parent.prototype;
Run Code Online (Sandbox Code Playgroud)
是不是实现继承的最佳方式?而不是让孩子"类"引用父原型,也许将父原型复制到子原型更有意义?
_.extend(child.prototype, parent.prototype);
Run Code Online (Sandbox Code Playgroud) 我的代码如下.
var Main = function () {
var a, b, c, d;
a = 1;
b = true;
c = undefined;
var _private = function () {
return 'Function with Private acceess';
};
this.getPublic = function () {
return 'Function with Public access';
};
this.getPrivate = function () {
_private();
};
};
var o = new Main();
console.log(o.getPublic());
console.log(o.getPrivate());
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我试图通过public方法访问Main对象o的私有方法getPrivate().但在控制台中的结果是
undefined
Run Code Online (Sandbox Code Playgroud)
为什么_private没有返回所需的值?
javascript ×10
closures ×2
constructor ×2
inheritance ×2
prototype ×2
angularjs ×1
mongodb ×1
node.js ×1