Lodash.js – constant() function

A simple Lodash.js example of the constant() function, which creates a function that returns a value.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen example"/>
    <title>Tizen lodash.js example</title>
    <script src="js/lodash.min.js"></script>
</head>
<body>
<div id="first">
</div>
<br>
<div id="second">
</div>

<script>

    var firstDiv = document.getElementById('first');
    var secondDiv = document.getElementById('second');

    var firstValue = { 'amount':'25' };

        firstDiv.innerHTML = "Having an object:<br><br>";
        firstDiv.innerHTML += JSON.stringify(firstValue);


    var result = _.constant(firstValue);

        secondDiv.innerHTML = "...we will use the constant function, to create a function variable which will return a value... and we get:<br><br>";
        secondDiv.innerHTML += "firstValue === " + JSON.stringify(firstValue) + " <br><br>";
        secondDiv.innerHTML += "result() === " + JSON.stringify(result()) + " <br><br>";
        secondDiv.innerHTML += "( result() === firstValue ) ---> " + (result() === firstValue);

</script>
</body>

</html>

Responses

0 Replies