javascript - Access of function in jQuery scope -
i want build function outside jquery scope:
(function($) { function myobject() { console.log('foo'); }; }(jquery)); var $my_object = new myobject();
but function myobject not accessible :
referenceerror: myobject not defined
however, if build function in scope, it's working:
(function($) { function myobject() { console.log('foo'); }; var $my_object = new myobject(); }(jquery));
foo
how access myobject outside scope ?
i not recommend can want returning functions part of object , assigning iife variable this
var library = (function ($) { var exports = {}; var private = 'see cant this'; var myobject = exports.myobject = function (_in) { console.log(_in); }; var another_func = exports.sum = function (a, b) { console.log(a + b); }; return exports; }(jquery)); library.myobject('foobar'); // "foobar" library.sum(3, 5); // 8 console.log(private); // uncaught referenceerror: private not defined
Comments
Post a Comment