C'è qualche ragione per cui dovrei usare $('#x>div').get(1)
quando invece potrei usare solo $('#x>div')[1]
? C'è una differenza?jQuery: array zero vs function get zero: [0] vs get (0)
risposta
No, nessuna differenza. jQuery contiene tutti i nodi DOM in una matrice.
$().get(1)
=== $()[1]
fonte --jQuery snippet--
get: function(num) {
return num == null ?
// Return a 'clean' array
this.toArray() :
// Return just the object
(num < 0 ? this[ this.length + num ] : this[ num ]);
},
Come si può vedere, .get()
senza argomenti tornerà tutti i nodi come Array. Questo non può essere realizzato con parentesi.
No, e le prestazioni è about the same perché la creazione di un oggetto jQuery domina gamma/funzione di tempo di accesso:
Browser get Ops/sec array Ops/sec #tests
Chrome 9 20,555 22,671 2
eh .. Mi aspetto che il metodo dell'array sia ** più ** efficiente in quanto non dovresti passare attraverso la funzione. –
Whoops, Oh, è .. Scusa pensavo in termini di millisecondi spesi, non operazioni al secondo –
Il tuo confronto è oscurato dal fatto che nel tuo test stai facendo anche un'operazione comparativamente pesante (creando un oggetto jQuery e selezione DOM). Se rimuovi quelli dal test, la differenza è mostrata enorme. http://jsperf.com/get-vs-array/2 'array: 123,366,553/get: 4,062,520' Quindi @ l'aspettativa di George era assolutamente corretta. – user113716
- 1. jQuery: eq() vs get()
- 2. È zero (-0) equivalente a zero (0) in C#
- 3. jquery $ .post() vs $ .get()
- 4. jQuery ajax() vs get()/post()
- 5. exit (0) vs return 0
- 6. Come riempire una matrice con zero (0)
- 7. JDBC get/setObject vs. get/setSpecificType
- 8. Cosa significa zero in Handler.sendEmptyMessage (0)
- 9. CoInitialize (zero) e CoInitializeEx (0, COINIT_MULTITHREADED) differenza
- 10. Come includere "zero"/"0" in COUNT aggregato?
- 11. Terminazione stringa - char c = 0 vs char c = '\ 0'
- 12. 0 vs '0' con gli operatori booleani
- 13. java.lang.StringIndexOutOfBoundsException: index = 0 lunghezza = 0 nel database SQLite get
- 14. Firmato zero linux vs windows
- 15. Perché utilizzare jQuery (selettore) .get (0) anziché jQuery (selettore) [0] per ottenere l'elemento DOM?
- 16. System.exit (0) vs JFrame.EXIT_ON_CLOSE
- 17. Zero in double vs char * ambiguity
- 18. Usa .get (0) o .html() per restituire HTML con jQuery
- 19. "% s"% format vs "{0}". Format() vs "?" Formato
- 20. Modulo Operatore vs Zero (re: fonte arc4random_uniform)
- 21. GET vs. POST (elaborazione modulo)
- 22. Django vista: get_context_data (vs) get()
- 23. Differenza tra "get' VS 'getProperty'
- 24. nuova IntPtr (0) vs. IntPtr.Zero
- 25. Python pop() vs pop (0)
- 26. schema: nessuno VS schema: 0
- 27. async Attività <HttpResponseMessage> Get VS HttpResponseMessage Get
- 28. np.full (dimensione, 0) vs. np.zeros (dimensione) vs. np.empty()
- 29. apt-get update' restituito un codice diverso da zero: 100
- 30. Stato HTTP 0 da AngularJS Get per JSON
Questo è un * esattamente * duplicato, ma non ho il link. –
Ecco perché ho spiegato le parole. Quindi sarebbe più facile trovarlo in seguito con una ricerca. Ho appena spiegato un po 'di parole in più se è d'aiuto. @ pst –