javascript - Placing instance properties in prototype constructors, good or bad? -
I have just finished reading very good articles on prototype succession in JavaScript, but amazed to see that the author is against the properties How strongly was defined in the prototype.
A common mistake in creating objects for the prototype series, programmers who come from classical OOP anyway, define the same qualities above in the series because they exist for all instances. We feel the need to define the property as the intangible object describes an interface. Nevertheless, there is no point in defining a prototype in an asset that exists from the object that descends from it. Javascript is not like Java: You do not declare base object variables, which will be different from all instances of descent. It will be defined where you declare only those variables.
Take the name of our animals as property Since every animal has a name, therefore it is natural to regard this property as normal for everyone, and it defines it as a common denominator, which is a prototype of a cattle. The thing is, there is no name for the animal. The example of a dog is a name.
In Javascript, you can not say that an animal has a name. Animals are an object, not definition, even if we use it the same way. And in that object there is no name property, why there is no name of animal, then why do animals have names in the methods? Since the animal is intangible: it is not intended to use it itself, it will never refer to the animal in the animal, whatever the object descends for example from animals, dino. And Dino has a name.
If I have very complex squares, then there are dozens of properties in common. I do not know that it is better to duplicate those properties and work that gets installed in every instant derivative orbit, Once the base class can be used, even if that base class means 'abstract'.
For example:
function analysis (args) {Args = Args || {}; Import imported properties from // extracts this.description = args.description; This.host = args.host; This.source = args.source; This .identifier = argsidentifier; This.vendor = args.vendor; This.agent = args.agent; // etc ...} function portfolio analysis (args) {analysis call (this, args); Args = args || {}; This.portfolio = args.portfolio; This.author = args.author; // etc ...} PortfolioAnalysis.prototype = Object. Create (analysis. Prototype); Portfolio Analysis Prototype Constructor = Portfolio Analysis; Function treaty analysis (args) {analysis call (this, args); Args = args || {}; This.treaty = args.treaty; This.terms = args.terms; // etc ...} treaty analysis Prototype = object Create (analysis. Protocol); Treaty analysis Prototype Constructor = Treaty Analysis; // etc ... So the article is saying that I should paste the start code for the attribute code description
, host
, source
, Etc., and remove it from the base class. I do not know why it is better, especially if a bunch of complex general logic is creating such objects using those shared properties, so bad about defining them in the base class , And if it is too bad, then there is no way around it that does not include code repeats or different '.init ()' method '?
Then the article is saying that I should paste the initialization code for the property description, the host , Source, etc., and removes it from the base class.
No, your code should be exactly the same way.
And that article is saying that such as description
, host
etc. should be placed on examples (like new threat analysis (.. .)
, or even new analysis (...)
), but not on analysis prototype
- just What are you doing? Some people are "default", e.g. Empty, identifier
and etc. on the analysis.prototype
because they want to "announce" that the example of each analysis should be an identifier. This article is bullshit, as the article tells.
Analysis
It is ok to share your initialization behavior in the constructor (as mentioned in the article, the shared work prototype series).
Comments
Post a Comment