JavaScript-Questions about the "table-driven approach". Thank you.

Source: Internet
Author: User
Everyone monkey good, recently saw the table-driven method, just encounter a similar situation, so try to achieve. But I don't think it's happening at all. Please look at the code. A total of three questions

    1. Is there a better way to define the table above? Although it is a manual list of all the possibilities, but it feels silly ... If there is more than one condition, or n conditions, then there is absolutely no way to manually enumerate ...

    2. If there's an elegant little notation?

    3. Why am I here return e[3]; Doesn't interrupt foreach.

Thank you for your reply.

function Getparentid (level, Focustype, AddType, select_id, parent_id) {//Focustype 0 = Node 1 = Event            AddType 0 = Node 1 = matter var default_id = 1; var array = [//[level,focustype,addtype,returnkey] [1, 0, 0, select_id],//Select the first node, select node, add node-- > returns the key of the current node [1, 1, 0, default_id],//Select the first node, option, add node--return 1 [1, 0, 1, select_id],//Select First layer node, select node, add Item--return key of current node [1, 1, 1, parent_id],//Select first node, optional, plus--Return parent node key [0,                 1, 0, parent_id],//non-first node, optional, add node--Return parent node key [0, 0, 0, select_id],//non-first layer node, select node, add node--return key of current node [0, 0, 1, select_id],//non-first node, select node, add matter--returns the key of the current node [0, 1, 1, parent_id]//non-first node, optional, plus            Events--Return to parent node key];            /* * What better way to define a table above question 1? * Although there is an artificial list of all the possibilities, but it feels silly ... If there is more than one condition, or n conditions, then there is absolutely no way to manually enumerate ... * */var id = default_id;            Array.foreach (function (e) {if (e[0] = = Level &&amp ; E[1] = = Focustype && e[2] = = AddType/* Question 2 here if there is an elegant point of writing it?                    */) {id = e[3]; /* Question 3 Why am I here return e[3]; Does not interrupt foreach?            */                }            });        return ID; }

Reply content:

Everyone monkey good, recently saw the table-driven method, just encounter a similar situation, so try to achieve. But I don't think it's happening at all. Please look at the code. A total of three questions

    1. Is there a better way to define the table above? Although it is a manual list of all the possibilities, but it feels silly ... If there is more than one condition, or n conditions, then there is absolutely no way to manually enumerate ...

    2. If there's an elegant little notation?

    3. Why am I here return e[3]; Doesn't interrupt foreach.

Thank you for your reply.

function Getparentid (level, Focustype, AddType, select_id, parent_id) {//Focustype 0 = Node 1 = Event            AddType 0 = Node 1 = matter var default_id = 1; var array = [//[level,focustype,addtype,returnkey] [1, 0, 0, select_id],//Select the first node, select node, add node-- > returns the key of the current node [1, 1, 0, default_id],//Select the first node, option, add node--return 1 [1, 0, 1, select_id],//Select First layer node, select node, add Item--return key of current node [1, 1, 1, parent_id],//Select first node, optional, plus--Return parent node key [0,                 1, 0, parent_id],//non-first node, optional, add node--Return parent node key [0, 0, 0, select_id],//non-first layer node, select node, add node--return key of current node [0, 0, 1, select_id],//non-first node, select node, add matter--returns the key of the current node [0, 1, 1, parent_id]//non-first node, optional, plus            Events--Return to parent node key];            /* * What better way to define a table above question 1? * Although there is an artificial list of all the possibilities, but it feels silly ... If there is more than one condition, or n conditions, then there is absolutely no way to manually enumerate ... * */var id = default_id;            Array.foreach (function (e) {if (e[0] = = Level &&amp ; E[1] = = Focustype && e[2] = = AddType/* Question 2 here if there is an elegant point of writing it?                    */) {id = e[3]; /* Question 3 Why am I here return e[3]; Does not interrupt foreach?            */                }            });        return ID; }

Question 1, 2:

One of the items in the table is information that is used to match patterns. This information is simple can be [1, 0, 0] represented by such a pure data (the following if the essence of a data into the behavior of the interpreter), complicated than the use of anonymous functions

Question 3. Anything you return will not let foreach break

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.