Code Monkey home page Code Monkey logo

angular-1's Issues

Comments on hw-1

Hsin -

Below are some comments on the first assignment. Just some stylistic things to watch out for:

apply.js

var _nycda = _nycda || {};
_nycda.apply = function (item, property, value) {
    /* Combine these using an && */
    if (item && property) {
        /* In your version all the variables below are in global scope.
         This is fine for these tests, but dangerous in the course of
         normal application development, try to prepend with var */
        var prop_parse = property.split(".");

        /* small, but you could make this simpler by just referencing
         length at each point */

        /* Also, you can just throw the first item into the new array */
        var myarray = [item];

        var ct = 0;
        for (var a = 0; a < prop_parse.length; a++) {
            if (!item.hasOwnProperty(prop_parse[a])) {
                ct++;
                if (ct >= 2) throw "Error";
            }
            else {
                myarray[a + 1] = item[prop_parse[a]]
                item = item[prop_parse[a]]
            }
            if (a == prop_parse.length - 1) {
                myarray[a + 1] = value;
            }
        }
        for (var b = prop_parse.length - 1; b >= 0; b--) {
            myarray[b][prop_parse[b]] = myarray[b + 1]
        }
        return myarray[0]

    }

    /* You can just short circuit down here if either of the upper ifs don't pass */
    return item
};

get.js

var _nycda = _nycda || {};
_nycda.get = function (item, property) {
    if (item && property) {
        /* Same comment about avoiding the global variables within the function, use var */
        var prop_parse = property.split(".")
        for (var a = 0; a < prop_parse.length; a++) {
            if (item.hasOwnProperty(prop_parse[a])) {
                item = item[prop_parse[a]]
            }
            else {
                return undefined
            }
            if (a == prop_parse.length - 1) {
                return item
            }
        }
    }
    return item;
};

/* Another way to cleverly do this one */
_nycda.get = function (item, property) {
    if (item && property) {
        /* Same comment about avoiding the global variables within the function, use var */
        property = property.split(".")
        while (property.length) {
            if (item) {
                item = item[property.shift()];
            } else {
                break;
            }
        }
    }
    return item;
};

conditional.js

var _nycda = _nycda || {};
_nycda.conditional = function (condition, runIfTrue) {
    /* May be best to actually check if this is a function, bit more robust */
    if (typeof runIfTrue === 'function') {
        if (condition == null) {
            return runIfTrue;
        }

        return function () {
            if (condition.apply(null, arguments)) {
                return runIfTrue.apply(null, arguments)
            }
            else {
                return (function () {
                    return undefined
                })();
            }
        };

    } else {
        throw "Hey don't do that";
    }
};

/* Otherwise, very good */

Thanks Hsin. You are quite excellent at this. Your apply implementation was very clever, I have never seen that one before.

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.