Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I assume you mean to compare it to:

    el.email = (function(){
        var input = document.createElement('input');
        input.type = "email";
        return input;
    })();
...or something similar. Your could also use createContextualFragement or a little function that uses innerHTML, both of which parse a string into HTML (which is problematic). Or you could use a templating library, of which there are many and this is one.


    el.email = document.createElement('input');
    el.email.type = "email";


    el.email = Object.assign(document.createElement('input'),{type:'email'});


el.email = { document.createElement('input'), ...{ type: 'email' }}


I know about the spread operator, but I'm pretty sure that's invalid. `var foo = { document.createElement('input') };` doesn't work, spread or no spread after it.


Ow man.. I saw the Object.assign example and thought i don't like it because it requires an extra polyfill. I can just use a spread and let babel handle it.

If anything it should be `var email = {...document.createElement('input'), type: 'email'}`

But that would result in `Object.assign({}, document.createElement('input'), { type: 'email' })`

Got 4 up votes though..


What supports this feature and how is it implemented?



(Too long to edit my other reply)

Also used was object destructuring: http://odetocode.com/blogs/scott/archive/2014/09/11/features...


$('<input type="email">')


I recently had the pleasure of tracking down the source code for how this works. There are a few methods, but the main one is at least a hundred lines long.


Why did you wrap that in a closure / immediately invoked function expression?


It's a way of hiding local variables. Old "var" variables are function scoped, so you can't even hide them in if-blocks or for-blocks, which makes scope leakage even worse.

If you're a JS programmer you should definitely read Crockford's famous JS book, it explains a lot of JS patterns and the rationale behind them.


Why compare it to that? There is no reason to avoid innerHTML anymore - it's not slow at all.

There is rarely a reason to build HTML using createElement, it's only sometimes needed. The majority of the time just write HTML - it's much easier.

> Or you could use a templating library

Or just use the built in <template> tag.


Not slow compared to what? Creating elements with the DOM API is faster than innerHTML roughly by a factor of 4 on Chrome Canary last I benchmarked it, DOM mutations with bindings are even faster. There is a reason why React switched from innerHTML to generating DOM.


>Creating elements with the DOM API is faster than innerHTML roughly by a factor of 4 on Chrome Canary last I benchmarked it

yes, almost by a factor of 4 :D

check out "render" test cases:

https://cdn.rawgit.com/localvoid/dd96890b82f1d1268df34330b14...


Template libraries are fine, but there are cases where you hope you could just write JavaScript..




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: