Javascript scoping for object -
i in javascript object, how possible declare 1 variable in 1 scope; mean merely variable , start using object assigning properties "inside" other functions. how scoping case work?
https://github.com/ariya/esprima/blob/master/esprima.js
in code, extra variable declared without being given properties:
var token, extra; and starts being used object follows:
function addcomment(type, value, start, end, loc) { var comment, attacher; assert(typeof start === 'number', 'comment must have valid position'); // because way actual token scanned, comments // (if any) skipped twice during lexical analysis. // thus, need skip adding comment if comment array // handled it. if (state.lastcommentstart >= start) { return; } state.lastcommentstart = start; comment = { type: type, value: value }; if (extra.range) { comment.range = [start, end]; } if (extra.loc) { comment.loc = loc; } extra.comments.push(comment); if (extra.attachcomment) { attacher = { comment: comment, leading: null, trailing: null, range: [start, end] }; extra.pendingcomments.push(attacher); } } the closest example can initiated object following function:
function tokenize(code, options) { var tostring, token, tokens; tostring = string; if (typeof code !== 'string' && !(code instanceof string)) { code = tostring(code); } delegate = syntaxtreedelegate; source = code; index = 0; linenumber = (source.length > 0) ? 1 : 0; linestart = 0; length = source.length; lookahead = null; state = { allowin: true, labelset: {}, infunctionbody: false, initeration: false, inswitch: false, lastcommentstart: -1 }; = {}; // options matching. options = options || {}; // of course collect tokens here. options.tokens = true; extra.tokens = []; extra.tokenize = true; // following 2 fields necessary compute regex tokens. extra.openparentoken = -1; extra.opencurlytoken = -1; extra.range = (typeof options.range === 'boolean') && options.range; extra.loc = (typeof options.loc === 'boolean') && options.loc; if (typeof options.comment === 'boolean' && options.comment) { extra.comments = []; } if (typeof options.tolerant === 'boolean' && options.tolerant) { extra.errors = []; } if (length > 0) { if (typeof source[0] === 'undefined') { // try first convert string. fast path // old ie understands string indexing string // literals , not string object. if (code instanceof string) { source = code.valueof(); } } } try { peek(); if (lookahead.type === token.eof) { return extra.tokens; } token = lex(); while (lookahead.type !== token.eof) { try { token = lex(); } catch (lexerror) { token = lookahead; if (extra.errors) { extra.errors.push(lexerror); // have break on first error // avoid infinite loops. break; } else { throw lexerror; } } } filtertokenlocation(); tokens = extra.tokens; if (typeof extra.comments !== 'undefined') { tokens.comments = extra.comments; } if (typeof extra.errors !== 'undefined') { tokens.errors = extra.errors; } } catch (e) { throw e; } { = {}; } return tokens; } but still inside function, not in same scope
var token, extra; how possible declare variable , instantiate properties inside function? how shared between different scopes? once given properties in 1 function, shared other scopes such scope in function? confusing.
line #3658 sets empty object:
extra = {};
Comments
Post a Comment