在Javascript中使用Prototype有什么好处?

发布于 2024-12-28 07:35:00 字数 536 浏览 1 评论 0原文

我阅读了 crockford 关于私有成员的页面 http://javascript.crockford.com/private.html Javascript 并得到了一个可能有些相关的问题。开发人员为什么要使用 Prototype?

例如,

例如,我可以这样做

var Foo = new Object();
Foo.bar = function() { alert('Its a bar'); };
var x = Foo;
x.bar();

而不是

var Foo = function(){};
Foo.prototype.bar = function(){alert('Its a bar');};
var x = new Foo();
x.bar();

这两个实现都做同样的事情。一个与另一个有何不同?这会以任何方式影响继承吗?

I read crockford's page on private members http://javascript.crockford.com/private.html in Javascript and got a question which maybe somewhat related. Why should a developer use Prototype?

For example,

For example, I can do this

var Foo = new Object();
Foo.bar = function() { alert('Its a bar'); };
var x = Foo;
x.bar();

instead of

var Foo = function(){};
Foo.prototype.bar = function(){alert('Its a bar');};
var x = new Foo();
x.bar();

Both of these implementations do the same thing. How is one different from the other? Does this affect inheritance in any way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2025-01-04 07:35:00

当您使用原型模式时,仅存在添加到原型的属性的一个实例。

// Lets create 1000 functions which do the same thing
for (var i=0;i<1000;i++) {
    Foo = new Object();
    Foo.bar = function() { alert('Its a bar'); };

    var x = Foo;
    x.bar();
}

// This is the same as #1, but is more common
function Foo() {
    this.bar = function () { alert('It\'s a bar'); };
}
for (var i=0;i<1000;i++) {
    var x = new Foo;
    x.bar();
}

// Lets create 1 function
var Foo = function(){};
Foo.prototype.bar = function(){alert('Its a bar');};
for (var i=0;i<1000;i++) {
    var x = new Foo();
    x.bar();
}

原型模式的缺点是无法访问私有成员。

// Lets create 1 function
var Foo = function(){
    var private = 4;

    this.baz = function () {
        alert(private);
    }
};
Foo.prototype.bar = function(){alert(private);};

var x = new foo;
x.bar(); // error; `private` is undefined
x.baz(); // ok

When you use the prototype pattern, only one instance of attributes you add to the prototype exist.

// Lets create 1000 functions which do the same thing
for (var i=0;i<1000;i++) {
    Foo = new Object();
    Foo.bar = function() { alert('Its a bar'); };

    var x = Foo;
    x.bar();
}

// This is the same as #1, but is more common
function Foo() {
    this.bar = function () { alert('It\'s a bar'); };
}
for (var i=0;i<1000;i++) {
    var x = new Foo;
    x.bar();
}

// Lets create 1 function
var Foo = function(){};
Foo.prototype.bar = function(){alert('Its a bar');};
for (var i=0;i<1000;i++) {
    var x = new Foo();
    x.bar();
}

The prototype pattern has the disadvantage of not being able to access private members.

// Lets create 1 function
var Foo = function(){
    var private = 4;

    this.baz = function () {
        alert(private);
    }
};
Foo.prototype.bar = function(){alert(private);};

var x = new foo;
x.bar(); // error; `private` is undefined
x.baz(); // ok
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文