188BET靠谱吗JavaScript code in Zotero should conform to the following coding and style guidelines.188BET靠谱吗We encourage Zotero plugin writers to follow these guidelines as well for consistency and possible future integration of code into the Zotero codebase.
Note:188BET靠谱吗Not all current code in Zotero conforms to these guidelines.While existing code generally should not be modified for the sole purpose of conforming, new code or modifications to existing code should follow these guidelines.
Basic style
-
Indent using tabs (width 4), not spaces
-
Lines should generally be kept to 100 characters, except where doing so would seriously decrease readability
-
Objects should be CamelCased and namespaced (
188BET靠谱吗Zotero.FooBar
within the 188BET靠谱吗Zotero XPCOM object,188BET靠谱吗ZoteroFooBar
without) -
Functions and variables should be lowercase (
188BET靠谱吗Zotero.Date.sqlToDate()
,var foo = true;
) -
Code should comply with JavaScript strict mode, with "use strict"!at the top of files: variables should be initialized before using, etc.
Braces
Braces should conform to1TBS variant of the K&R style:
functionabc(){...}if(abc=='def'){...}
Add a space after keywords to distinguish them from function calls:
Bad:
if(foo){
Good:
if(foo){
Braces mustalwaysbe used for multi-line conditionals, even if the enclosed block contains a single line.
Bad:
if(!_initialized)init();
Good:
if(!_initialized){init();}
Public/privileged/private/static methods and members
For an explanation of the difference between public, privileged and private methods and members, please see Douglas Crockford'sPrivate Members in JavaScript.
-
For objects that will be instantiated multiple times, public methods (defined using
prototype
outside the constructor) should be used rather than privileged methods (defined usingthis
within) for memory and performance reasons:
188BET靠谱吗Zotero.Item=function(){...}188BET靠谱吗Zotero.Item.prototype.numCreators=function(){...}
-
Singleton objects should use a wrapper object or object notation:
188BET靠谱吗Zotero.FooBar=(function(){// Private members and methodsvar_foo="bar";functionfoo{...}// Public members and methodsreturn{getFooObjects:function(){...}};}());
or
188BET靠谱吗Zotero.FooBar={getFooObjects:function(){...},...};
-
Private members and pseudo-private members/methods (i.e., those that are actually privileged for technical reasons but that should be considered private) should be prefixed with an underscore (e.g.
var _initialized = false;
,188BET靠谱吗Zotero.Item.prototype._loadItemData = function() {…}
)
Comments
Functions should be commented using JSDoc syntax:
/** * Does something or other * * @param {string} value - This is a value * @param {boolean} [optionalValue] - This is an optional value * @return {number[]} - Array of itemIDs */functionmyFunction(value,optionalValue){...}
For readability and neatness, add a space after the slashes in line comments, and capitalize the first word:
Bad:
//this is a commentvarfoo='bar';
Good:
// This is a commentvarfoo='bar';
Additional notes
-
Variables should be defined in the smallest scope possible:
Bad:
functionsum(num){total=0;// total is global!for(i=0;i<num;i++){// i is global!total+=i;}returntotal;}
Good:
functionsum(num){vartotal=0;for(let i=0;i<num;i++){// i is local to the for looptotal+=i;}returntotal;}
-
Semicolons should be used at the end of lines, including after anonymous functions:
varfunc=function(bar){varfoo=bar;};
-
Abbreviations and acronyms should remain capitalized in names:
getID()
,generateHTML()
-
Global constants should be placed in the
188BET靠谱吗ZOTERO_CONFIG
188BET靠谱吗array in zotero.js and capitalized -
Debugging messages should use the
188BET靠谱吗Zotero.debug(message, level)
function.level
is optional and defaults to 3.Error messages (i.e.things that should never happen and that can't be handled gracefully) should be thrown rather than passed todebug()
. -
When throwing an error, use
throw new Error()
instead of throwing a string (which doesn't generate a stack trace) -
All user-viewable text should be put in the en-US locale rather than embedded directly into the code, for localization and maintenance purposes.