简体   繁体   English

我们可以在对象构造函数内分配一个公共方法吗? (JavaScript的)

[英]Can we assign a public method inside a object constructor ? (javascript)

I am reading about prototypes in javascript, In a article http://phrogz.net/js/classes/OOPinJS.html I read that we can not assign public methods inside a object constructor in javascript ? 我正在阅读有关javascript中的原型的信息,在文章http://phrogz.net/js/classes/OOPinJS.html中,我了解到我们无法在javascript中的对象构造函数中分配公共方法? How prototypal methods are different from static methods and what are the advantage of using them ? 原型方法与静态方法有何不同?使用它们的优点是什么?

JavaScript isn't really well suited for most OOP concepts and paradigms, especially once you try to emulate inheritance. JavaScript并不是非常适合大多数OOP概念和范例,尤其是当您尝试模仿继承时。 Rather than think of prototype vs "privileged" methods in OOP terms, you should think of things in terms of how JavaScript instantiates objects. 与其以OOP术语来考虑原型方法还是“特权”方法,不如以JavaScript实例化对象的方式来思考问题。 Take this simple "class": 采取这个简单的“类”:

var id = 0;

function myClass()
{
     var that = this;
     id++; //closure, each new instance gets a unique id
     this.id = id;

     this.toString = function()
     {
         return that.id.toString();
     }
}

And this class is instantiated like so: 此类的实例化如下:

var classInstance = new myClass();

This isn't a pattern I would necessarily recommend, the point is to illustrate that for each instantiation, each instance gets its own unique toString function . 这不是我必须推荐的模式,重点是说明对于每个实例, 每个实例都有自己独特的toString函数 That means if you instantiate 100 classInstances , and you change toString on one of them to do something else, only that one instance will have that new functionality. 这意味着,如果实例化100个classInstances ,并且在其中一个实例上将toString更改为其他功能,则只有一个实例将具有该新功能。

That also means that for every instance, every privileged method is also instantiated alongside with it. 意味着,对于每个实例,每个特权方法也都与它一起实例化。 If you are instantiating a lot of instances, that can make a big performance difference. 如果要实例化许多实例,则可能会产生很大的性能差异。 I had a case where I saw a measurable speed improvement by converting my privileged methods to prototype methods. 我遇到了一种情况,通过将特权方法转换为原型方法,可以看到可测量的速度改进。

Speaking of prototype methods, here's what that might look like: 说到原型方法,可能是这样的:

var id = 0;

function myClass()
{
     id++; //closure, each new instance gets a unique id
     this.id = id;
}

myClass.prototype.toString = function()
{
    return this.id.toString();
}

In this case no matter how many myClasses you have, you only instantiate the toString method once. 在这种情况下,无论您有多少个myClasses ,都只实例化一次toString方法。 And if it changes, it changes for all myClasses . 如果更改,则所有myClasses都会更改。

Personally, I use privileged methods in most of my JavaScript classes because it looks cleaner and only bother with the prototype chain if I know it's going to be instantiated a huge number of times. 就我个人而言,我在大多数JavaScript类中都使用特权方法,因为它看起来更简洁,并且只有在知道要实例化大量次数的情况下,才对原型链感到困扰。 Also being able to access private variables allows you to have some semblance of information hiding vs being forced to make any accessed variables public. 此外,能够访问私有变量还使您可以隐藏一些信息,而不必强制将所有访问变量公开。

I read that we can not assign public methods inside a object constructor in javascript? 我读到我们不能在javascript的对象构造函数内分配公共方法?

Yes, the article refers to this: 是的,本文引用了以下内容:

function MyObj(name)
{
    this.name = name;
}

MyObj.prototype.sayHello = function() {
    alert('hello ' + this.name);
}

new MyObj('world').sayHello();

As you can see, the public method sayHello() is declared in the prototype, which is done outside of the constructor. 如您所见,公共方法sayHello()在原型中声明,该方法在构造函数之外完成。 This is just how JavaScript works. 这就是JavaScript的工作方式。

How prototypal methods are different from static methods and what are the advantage of using them ? 原型方法与静态方法有何不同?使用它们的优点是什么?

Prototypal methods are only "attached" to objects. 原型方法仅“附加”到对象。 For static methods you need to use this construct: 对于静态方法,您需要使用以下构造:

var MyStaticThing = {
    name: 'world',
    sayHello: function() {
        alert('hello ' + this.name);
    }
}

MyStaticThing.sayHello();

"the advantage of using them"...which one are you referring? “使用它们的优势” ...您指的是哪一个? Generally the advantage is going to be using a prototypal method (and object construction generally), over something like static or classical methods in javascript. 通常,与javascript中的静态或经典方法相比,优点是将使用原型方法(通常是对象构造)。 The answer you are looking for is really too long to address here, but the short answer is that javascript is based off a prototypal object inheritance system (meaning objects can be created on the fly and may inherited one to the other), as opposed to a classical system (objects may only inherit from classes, ie Java, C++, etc.) 您正在寻找的答案真的太长了,无法在此处解决,但是简短的答案是javascript基于原型对象继承系统(意味着对象可以动态创建,并且可以彼此继承),而不是经典系统(对象只能从类(即Java,C ++等)继承)

While you may create objects in javascript in a classical way -- because the language is that flexible -- it is a bad and confused way to do it. 尽管您可以用经典的方式在javascript中创建对象-因为该语言是如此灵活-但它是一种糟糕而混乱的方式。 Prototypical object construction allows you do important and good things like data hiding, access to super methods, etc. Like I said this is a verbose subject, really to big to be taken on in a little text box. 原型对象构造使您可以做一些重要的事情,例如数据隐藏,访问超级方法等。就像我说的那样,这是一个冗长的主题,确实很大,需要在一个小的文本框中进行处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以在javascript中将对象分配到cookie中吗? - can we assign object into cookie in javascript? 如何在 javascript Object.assign 方法中使用所有函数参数 - How can I use all function arguments inside javascript Object.assign method 我们可以在没有jQuery的情况下将对象分配给纯JavaScript中的html元素吗 - Can we assign an object to a html element in plain JavaScript without jQuery 从自我对象javascript内部调用的公共方法? - public method called from inside self object javascript? 我们如何使用JavaScript中的事件处理程序在public function内部调用public function - How we can call a public function inside public function using event handler in javascript 为什么我们需要在构造函数中使用apply方法来调用原型对象上定义的任何方法? - Why do we need apply method inside the constructor to invoke any method defined on the prototype object? 如何从javascript对象执行公共方法 - How can I execute public method from javascript object 我可以在JavaScript的方法内添加对象吗? - Can i add a object inside a method in JavaScript? JavaScript 我们可以将 then() 中应该包含的内容带入带参数的方法中并调用它 - JavaScript can we carry what should be inside then() into method with parameters and call it 如果我们分配给其他对象,对象的 JavaScript 传递引用在函数内部没有影响? - JavaScript Pass By Reference for objects does not have effect inside function if we assign in to other object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM