Jonas said:
Richard Cornford escreveu:
I just gave an example of where I used,
But not a good example as the same discrimination can be achieved with
typeof. If an effort is to be made to preserver the - constructor -
property in an inheritance tree then the example that may justify it
should relate to testing objects not string primitives.
I wasn't supposed to give a complete example or explain
what was my code about, this only became needed when you
criticized it.
I may not have criticised it if you had managed to provide a example
that could not be trivially replaced with a more efficient alternative.
With the implication that using the - constructor - property to test the
value of a genuinely unknown type is not safe unless it follows another
test that ensures the type-conversion to object does not error out.
It was an idiot code to simulate overloading of functions
based on the types of the arguments.
That response seems disconnected from its context.
Because you are programming and the objects you use don't just appear
out of thin air.
Every problem has a different solution.
And for every good solution there are a dozen worse solution.
It's strange to impose your techniques as being the
right way of doing things.
In what sense could I impose any technique on you (short of your working
for my employer)? Most programmers are interested in how to do things
better, if you are not that is your problem, but I will still point it
out when I see it.
Ah, for me it's ok, like 0.0.toFixed()
If you want the string that results from calling - toFiexed - on a
Number object and you have a variable with a number primitive value to
start with then you have no better choice (except maybe doing the
implied type-conversion explicitly for clarity). But your method of
testing the type of string primitives is non-obvious and inefficient in
comparison to - typeof.
It makes total sense, I didn't read the spec, so I thought
everything was Object, even the native types.
Javascript's object type is a "native type".
But it makes sense to do it this
way too, since it will probably increase the speed.
Anyway, in my opinion it's not the case of saying it's
an overhead.
Needlessly brining an object into existence is an overhead. You can
argue that it is not a very large overhead but it is still an overhead.
Well, I will not agree with you that it's ok to ignore it, so
this discussion will have no end.
The discussion can have an end. You could attempt to demonstrate the
necessity and either succeed in demonstrating it (which would convince
me) or I could show that your necessity was an illusion and you would no
longer have any justification.
That probably would not work in practice as you don't seem to see that
you should not be programming things that you cannot justify.
If it's not useful in your opinion, ignore, I won't. I've
made a code using the constructor, maybe another person
have done it too, so if you change the expected behavior,
things get confused.
Why? I cannot change the code in the system you wrote.
I watched an old version, it didn't interested me. Anyway I don't
blame the author, he just made a library and by the way obtained
success/fame.
Infamy at least.
If a guilty is needed for spreading this crap, I can
only see the users.
Has the author told them that the library is not ECMAScript compatible?
Isn't it more likely the case that he is not aware himself?
This is bad, opinions shouldn't be ignored.
If people don't know what they are doing any opinions they express about
how it should be done cannot be informed and so should not be taken
seriously.
Such loop is slower too, but I think it shouldn't be killed.
It is not slower when enumeration the assigned values in a sparse array.
For example:-
var ar = [];
ar[5555555555555] = 5;
for(var n in ar){
// code here is only executed once.
}
for(var c = 0;c < ar.length;++c)[
// code here is executed 5555555555556 times,
// and ar[c] is undefined in most of those loops.
}
The former will be much faster.
For him it's ok to kill a language feature, but it's not
for me and probably for others :]
It is OK if you know you have done it and are willing to deal with the
consequences.
If you're making something to will be shared with people
or that will be managed by others in the future, then I'm
against killing features,
If the people using or maintaining the code know what they are dealing
with then it doesn't matter. They would be wrong to introduce code that
either broke because of the existing system, or broke the existing
system, but they would know not to do that.
everything should be conserved to avoid remaking working
codes. It's just my best practice rule, you don't need to
agree with that.
No I don't need to agree. I believe that not writing code that serves no
useful purpose if better practice.
You are editing the material you quote without marking your edits. That
is disingenuous.
If you/your client have conditions to manage that, it's ok.
You mean like requiring future developers to read the documentation (or
at least its introduction) before they start coding for the system?
Well, yes we have conditions to manage that.
I still
prefer to not do things that can damage others.
And you don't think encouraging people to waste their time would qualify
as damaging them?
Difficult = difficult to see a place to use it.
That is a characteristic of things that are unnecessary.
I can't say it's unnecessary, as well I can't say the eval
is unnecessary.
Eval is unnecessary, it is just occasionally a small/fast alternative to
doing your own tokenising and parsing. But easily the vast majority of -
eval - uses in the real world follow directly from a script author not
knowing, or preferring not to use, a better alternative. As a result -
eval - use if often indicative of poorly designed code. In the same way
code that thinks it needs to be so interested in object types that it is
looking at - constructor - properties is also a symptom of not knowing,
or preferring not to use, better alternatives, and also indicative of
poorly designed code.
This is your way of seeing things in JavaScript.
Yes, being able to see what is irrelevant help in narrowing down to what
is important.
It seems that for you it's fine to prototype the
Object if you don't have any for..in
statement.
Yes, you cannot do both without having to jump through hoops to mitigate
(and probably losing the benefits of for-in with sparse arrays along the
way), but either are reasonable in the absence of the other.
You give up a great deal of javascript's flexibility in taking that
position. And if you insist on worrying about the consequences of
aggregating code from diverse sources without understanding that code
you end up in a position where you don't extend prototypes out of fear
of the consequences, but you also cannot use for-in (at all, or without
testing each value retrieved) because others may have extended
prototypes.
There's no "when", I just pointed out a possibility.
If it is reasonable to ask "how I can create a new Father without
knowing the class of the variable" then there must be a "when". I knew
the type of the variable when I wrote;-
var o = new Father;
- so in order to be in a position where I don't know the type of the
variable there must have been a point when I stopped knowing its type.
If there was never a "when" your question becomes moot as I am never in
a position where I don't know the type of variable.
If it's not your way of doing things (unknown 'classes'),
fine.
Well, it certainly is my "way" to understand what the code I am writing
is doing.
I'm talking generally, so if it's useful for someone, then
it's enough for me, it doesn't matter if the person is doing
something wrong in your opinion, I just try to avoid messing
with external codes.
How is this "external code" going to have a problem with the class
definitions you create? It is not going to know of there existence and
if you are passing objects of your classes into it it is going to be
quite important that it does not have strong attitude about the types it
receives, else it will just reject your objects as being outside its
knowledge.
Here is the advantage of interfaces (and the ease with which any 'class'
defined in javascript can implement any interface at all, or have one
added to it with augmentation). All your external code has to do is
define an interface which it expects objects passed into it to implement
and then you create 'classes' with that interface. Now you can pass
objects back and forth without either side having to have a strong
interest in the types of the actual objects used.
Being able to contrive an example where - constructor -
may be useful has no impact on its usefulness (or lack
of usefulness) in code where it is not employed.
Ok, I already know your point of view, mine one is different.
Actually I didn't looked for it, I just needed to make that
code, and the constructor solved my problem ;]
I am left suspecting that your "problem" followed from
inappropriate code design and that actions that "solved" the
problem only plastered over a more fundamental issue
following from the design flaw.
Well, you're free to suspect.
I have little choice.
For me the code is ok, the other way (with typeof)
will require some changes that won't give me any real
benefit.
Changes would only be necessary because of the way you wrote it in the
first place.
Fine, but I'll use it again and again, even now, that I
know the real definition, because the main attribute for
me it's enclosing variables,
Didn't you say; "I still prefer to not do things that can damage
others"? Doesn't inappropriately applying technical terminology in front
of novices risk misleading them, and thus doing them harm.
so, when you see me talking closure again, you'll know the
meaning xD
The consequence will be repeated corrections, and in needing repeated
correction you will be depriving the more deserving of the time taken to
repeatedly correct you (thus harming them).
Will this kill my computer?
If you do it often enough.
I'll keep doing it as it looks nice for me :]
Aesthetics are rarely good criteria for code design decisions.
Creating a new wasteful empty object isn't something
that makes me feel worried.
Yes, that's my point of view.
No, it's a common sense that every code should be done
as efficient, small and clean as possible.
Not really, clarity if often an important criteria. The 'common sense'
is not to write code that does things that don't need doing and don't
have to be done. However, doing that does negatively impact code size,
efficiency and cleanness in addition to clarity.
But changing "(function(){})()" to "new function(){}" is
something that won't affect the efficiency of my codes, a
good idea can speed up things in a higher factor, but such
change, won't change in nothing, I prefer to keep the cute
way :]
No single action is likely to significantly impact performance, but here
we have been discussing various things and you:-
1. Want to spend time assigning - constructor - values regardless of
whether of not they will ever be used.
2. Would prefer ''.constructor == String over typeof '' == 'string',
regardless of the implied need for the type-conversion, and construction
of an unnecessary object as a consequence.
3. Want to use - new - when you only need the private scope that
executing a function expression will give you, regardless of the
superfluous extra object created as a consequence.
And that is; in every aspect of scripting we have discussed in this
thread you are preferring a less efficient/more wasteful approach than
you could use. Individually none is likely to make much difference, but
taken together, and particularly if this pattern of preferring the less
efficient is carried through the rest of your code, they will add up.
The "big idea" may have a significant impact upon performance, but not
habitually writing universally inefficient code might save you needing
the 'big idea' in the first place.
same effect = create something wasteful.
The execution of a function expression creates a number of objects,
exactly the same number as the execution of a constructor body implied
in the use of the - new - operator. Those objects are the price of
having a private scope and so if you want a private scope you pay that
price whatever you do. There is no waste in that, just a known cost.
Using the - new - operator on a function expression creates an
additional object, which is redundant if all you want is a private
scope.
I know they aren't equal, but for me there's no significant
difference that forces me to use one and not the other.
But that just means that your perception of aesthetics outweighs the
waste in needlessly creating a superfluous object in your mind.
Yes, such code is so strange, the "new" in front of the
function will destroy the mind of someone hahaha.
It won't destroy their mind, it will make then think of the creation of
objects, and, given that programmed action is normally driven by
imperative, look for the reasoning that makes this object creation
necessary/desirable.
It's soooo wrong, I'm feeling
ashamed of posting it :'/
If only I believed you could see that.
If nothing is being assigned and if the this isn't
stored anywhere, the object is wasteful, there's
nothing to think about it...
Yes, everything that is being done can be done without creating the
superfluous object by no greater change than the removal of the - new -
operator and the addition of some parentheses.
And if a person does know the language anything done in
an obtuse and inefficient way when a better alternative
is obvious will also take some time to understand,
because time would be spent trying to see the reason
for choosing the obscure and inefficient method over
the obvious one. And as there is no reason for using
your approach the time spent trying to identify the
reasoning could be indefinitely extended.
There's really no reason to do it, I do it because it
looks more pretty for me :]
Aesthetics may guide your authoring, others are likely to apply
reasoning while reading your code, and be mislead by your design
criteria.
Both doesn't matter for me, I program for fun... xD
Well your audience here are a mixture of experienced programmers and
novices (some of whom are planning on becoming experienced programmers,
and so would benefit from being guided in an appropriate direction).
This means I won't change my style to make the code
more readable or change the way I initialize things
because it creates a ghost object, whatever... I
change something only when it has a visible impact.
I'll keep my things safe, if you have manuals ordering people to
not do things and if they follow,
They will follow, or they won't stay employed for long.
you can program in the other way "if
it's not being used, you can change it" :]
PS: Again you made me lost a lot of time... I was going to
ignore this message, but I answered, but now I'm regretted,
so if you answer, I'll ignore it, since there's nothing useful
in this discussion, it's over, there's just a bunch of personal
opinions that don't match.
OK
Richard.