Git Product home page Git Product logo

Comments (3)

maksimr avatar maksimr commented on July 30, 2024

###enforcing-new.html

I do not think that in this example, the main thrust is to reduce the number of input characters. So you do protection inside the function constructor. Because if someone accidentally calls her without a new one, it can create global variables or fall with an error in strict mode.

###avoiding-implied-typecasting.html

I agree with you that in most cases this will be the construction that you described, but as I understand it, this example suggests that it is necessary to avoid an automatic type cast, as this can lead to errors as the beginners and experienced developers.

p.s. In you example you get error because some is not defined ;)

###for-loops.html
http://jsperf.com/for-loop-dom (this is a very rough test =) )
I think this example is designed for the most part to work with the tree of the document, as the re-conversion of length collections elements rather expensive operation (such as collections are alive). And if no difference I prefer cache variant.

###for-in-loops.html

  1. Unfortunately it is not(Personally came across this). Some DOM object does not has this property(or work incorrect).
  2. When you call someObject.hasOwnProperty It will be a look at the whole prototype chain until it reaches Object.prototype, additional time spent.
  3. It flows from 2. The object has this property can override anyone else.

###function-declarations.html

Well, as you may have noticed not only a function declaration that is determined before the execution of code, so more and overwrites all that already has that name, and even other function declaration (and it is not corrected during the execution of the function). And since they are determined to run the code it is not always and not for everyone obviously, too much is not always a good trick, especially if you are in command. Oh, and lastly, such a function can only be defined in the body of a function or program level, which is pretty much reduces the area of ​​their ads.

###conditionals.html

&& and || are logical operators, but some times you can use them how something like conditional expressions because they return value of operand.

###revelation.html and module.html

var fakeArray, MakeFakeArray = function () {};
MakeFakeArray.prototype = Array.prototype;

fakeArray = new MakeFakeArray();

(fakeArray instanceof Array); //true
({}).toString.call(fakeArray);//"[object Object]"

p.s. Надеюсь я правильно понял твои замечания и сам что-нибудь полезное ответил. =)

Maksim

from javascript-patterns.

Alexey-Raspopov avatar Alexey-Raspopov commented on July 30, 2024

###enforcing-new.html

99% of code's errors is sitting behind the monitor, right? :)

###avoiding-implied-typecasting.html

there are no strict mode in my example :P

###for-loops.html

Thanks for your test, I didn't know about this.

###function-declarations.html

If you work in team you can DISCUSS that moments and problems what you describe will disappear.

###revelation.html and module.html

To your example and to all your comments:

I almost agree with all your comments, but all problems what your have described can be avoid if you create standarts to you and your team, if you provide 100% code review for all commits and if you discuss code with your teammates

from javascript-patterns.

nmartynenko avatar nmartynenko commented on July 30, 2024

jQuery data anti-pattern

I suppose that this is not anti-pattern due $.data and $.fn.data may be used in different cases:

<div id="entryHolder" data-entry-id="1">
...content
</div>
var holder = document.getElementById("entryHolder");

alert($.data(holder, "entryId")); //shows undefined

alert($(holder).data("entryId")); //shows 1

//BUT!
alert($.data(holder, "entryId")); //shows 1
  • $.fn.data can be used in chainings, $.data - not
//legal
$(".selector")
   .data("test", "val") //returns jQuery object
   .slideUp()

//illegal
$.data(elem, "test", "val") //returns value
    .slideUp(); //throws exception
  • $.fn.data works with jQuery objects, meanwhile $.data works with DOM elements. Therefore if you had DOM element it's better using of $.data .
//bad practice
$(domElem).data("some", "value");

//preffered
$.data(domElem, "some", "value");

NOTE: It doesn't fluent to chainings.

from javascript-patterns.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.