myJs

阅读更多

1. run js right now

 

var wrapper = (function(){
    var foo = "foo";
    function f(){
        return foo;
    }
    return f;
})();

  

2. scrollWindow

 

function scrollWindow()
  {
//window.scrollTo(document.body.scrollLeft.toString(),document.body.scrollTop.toString());
  //window.scrollTo(0,0)
document.body.scrollTop = 0;
  }

 

 

 

3. dynamicGetDomObject

 

eval("document.yourform.yourInputNamePrefix_" + index);

 

 4. Public and Private

var MYLIB = function() {  
    var aPrivateProperty = true;
    var aPrivateMethod = function() {
    	// some code here...
    };
    return {
    	aPublicMethod : function() {
    		aPrivateMethod(); // okay
    		// some code here...
    	},
    	aPublicProperty : true
    };  
}();

MYLIB.aPrivateMethod() // not okay
MYLIB.aPublicMethod() // okay

 

function Restaurant() {
}

Restaurant.prototype = (function() {
    var private_stuff = function() {
        // Private code here
    };

    return {

        constructor:Restaurant,

        use_restroom:function() {
            private_stuff();
        }

    };
})();

var r = new Restaurant();

// This will work:
r.use_restroom();

// This will cause an error:
r.private_stuff();

 

format Date

console.log(d.getHours()+"-"+d.getMinutes()+"-"+d.getSeconds()+"-"+d.getMilliseconds());

 

你可能感兴趣的:(JavaSript)