简体   繁体   中英

using provide in closure library

I'm very newbie in closure, I'm reading the tutorial at: https://developers.google.com/closure/library/docs/tutorial and it says that

goog.provide('tutorial.notepad.Note');

is equivalent to

tutorial = tutorial || {};
tutorial.notepad = tutorial.notepad || {};
tutorial.notepad.Note = tutorial.notepad.Note || {};

basing on that fact, I assume that if use goog.provide('tutorial.notepad.Note');

then there is no need to use goog.provide('tutorial.notepad);

but the example uses both of them together. may somebody explain to me why?

goog.provide(namespace) will check each dot-delimited name starting on the left and create a property pointing to a new object literal equivalent to the tutorial excerpt you provided above. Therefore, you are correct that notepad.js does not technically need to include goog.provide('tutorial.notepad') , since goog.provide('tutorial.notepad.Note') will ensure that the object chain tutorial.notepad exists.

However, in addition to defining a Note object with member function makeNoteDom , notepad.js also defines a utility function makeNotes , which is a member of the tutorial.notepad namespace. By including goog.provide(tutorial.notepad) , it indicates that notepad.js provides package-level functionality in addition to defining a Note object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM