this.method = function(){} VS obj.prototype.method = function(){} [英] this.method = function(){} VS obj.prototype.method = function (){}

查看:144
本文介绍了this.method = function(){} VS obj.prototype.method = function(){}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一名c#/ .net开发者,我喜欢在业余时间用JavaScript来玩弄 - 创建我自己的库/框架等等。无可否认,它们并不多(实际上只不过是一组松散的函数),但目的是学习;不适合其他人使用。



我通常以这种方式扩展一个基本的JavaScript对象

 < code $ obj = function(){
//基本对象的东西
this.method = function(){
//其他的东西
};
返回此;
};

这允许我一起创建其他对象和链式方法,这实际上是光滑的:

  obj('arg1')。method(); 

两个例子: jQuery Knock-off 列表-Item Sorter



然而,我最近看到,在比我自己更多的函数代码中,对象通过这种方式实现了相同的功能:



$ $ p $ function obj(){
// stuff
}
obj.prototype.method = function() {
// stuff
};

示例: Reddit Chrome扩展



两种方式似乎都达到了相同的目的,我对这两种语法的看法都不是部分的。有没有一种比另一种更有用的特殊情况?
$ b

编辑

这些方法提供了什么使他们比其他人更受欢迎?请看下面的代码:

  var dice = function(sides){
this.roll(){
return 4 //保证是随机的
}
};

var d1 =骰子(6);
d1.roll()// 4;
var d2 =骰子(20);
d2.roll()// 4

d1和d2是不同的对象,因为它们在我看来?或者它们是指向一个对象(var dice)的指针/昵称?

解决方案

  this。 method = function(){}; 

仅适用于特定实例。

  Obj.prototype.method = function(){}; 

将适用于 Obj



尽管为了利用原型,您应该这样做

  var o = new Obj(); //请注意,旨在与新一起使用的函数应该大写

o.method();






骰子示例



我假设您打算在 dice()函数中返回这个



该示例并不常见,因为调用函数不会创建新对象。在你的骰子的情况下,你将为这个分配一个方法,该函数内部是 window 全局对象,然后返回它。



中结果将是相同的对象( window )。 d1 d2 ,方法 roll 将在第二次调用中重新分配。



要达到你想要的效果,你应该使用 new 来创建实例,如下所示:

  var d1 = new Dice(6); //记住大写在这里很重要
var d2 = new Dice(20);

这会创建2 roll 函数,这是正确的,但浪费内存,因为该功能可以通过做共享:

  Dice.prototype.roll = function(){/ *返回随机awesomeness * /}; 

希望澄清事情


As a c#/.net dev, I love to toy around with JavaScript in my spare time -- creating my own libraries/frameworks and such. Admittedly, they're not much (really nothing more than a loose collections of functions), but the purpose is to learn; not for other people to use.

I usually extend a basic JavaScript object this way

obj = function () {
    //basic object stuff
    this.method = function () {
        //other stuff
    };
    return this;
};

This allows me to create other objects and chain methods together, which is really slick:

obj('arg1').method();

Two Examples: jQuery Knock-off, List-Item Sorter

However, I have recently seen, in much more function code than my own, objects accomplish the same functionality this way:

function obj(){
    //stuff
}
obj.prototype.method = function () {
    //stuff
};

Example: Reddit Chrome Extension

Both ways seem to accomplish the same end, and I'm not partial to the look of either syntax. Is there a particular situation where one would be more useful than the other? What do these methods offer that makes them more desirable than the other?

Edit

Consider the following code:

var dice = function (sides) {
    this.roll(){
        return 4 //guaranteed to be random
    }
};

var d1 = dice(6);
d1.roll()  // 4;
var d2 = dice(20);
d2.roll()  // 4

Are d1 and d2 different objects, as they appear to me? Or are they pointers/nicknames to one object (var dice)?

解决方案

this.method = function(){};

Only works for that specific instance.

Obj.prototype.method = function(){};

Will work for every instance of Obj

Though in order to take advantage of prototype you should do

var o = new Obj(); // Note that functions intended to be used with "new" should be capitalized

o.method();


Dice Example

I'll assume you intended to return this in your dice() function.

That example is not really common, because calling a function does not create a new object. In your dice case you would be assigning a method to this, which inside the function is window the global object, and then returning it.

The outcome would be the same object (window) in both d1 and d2, with a method roll which would be reassigned in the 2nd call.

To achieve what you want you should create the instances with new, like this:

var d1 = new Dice(6); // remember capitalization is important here
var d2 = new Dice(20); 

This will however create 2 roll functions, which is correct but wastes memory since the function can be shared by doing:

Dice.prototype.roll = function() { /* return random awesomeness */ };

Hope that clarifies things

这篇关于this.method = function(){} VS obj.prototype.method = function(){}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆