Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active October 18, 2017 08:07
Show Gist options
  • Save xgqfrms-GitHub/cd117d20b7c087c0de820f6c3b542148 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/cd117d20b7c087c0de820f6c3b542148 to your computer and use it in GitHub Desktop.
!function(){} &JavaScript Logical Operators

!function(){} & JavaScript Logical Operators

http://taobaofed.org/blog/2015/10/28/try-catch-runing-problem/

!function(){} & IIFE & shorthand IIFE

https://stackoverflow.com/questions/9267289/what-does-function-in-javascript-mean

https://about.twitter.com/zh-hans/resources/buttons#follow

// shorthand/ 替代, 自我调用匿名函数:


!function(){
  // code
}();

//===

(function(){
  // code
})();

(function(){
  // code
}());

https://www.ecma-international.org/publications/standards/Ecma-262.htm

https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/

https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

let x_style = `
    color: #0f0;
    background: rgba(0,0,0,0.5);
    font-size: 2rem;
    border: 1px solid red;
    border-radius: 7px;
`;

let message = `IIFE`;

(function(){
    console.log(`%c message \n`, `${x_style}`, `${message} outer`);
})();

(function(){
    console.log(`%c message \n`, `${x_style}`, `${message} inner`);
}());



!function(){
    console.log(`%c message \n`, `${x_style}`, `${message} !`);
}();


You can also use + instead of !.

+function(){
    console.log(`%c message \n`, `${x_style}`, `${message} +`);
}();


-function(){
    console.log(`%c message \n`, `${x_style}`, `${message} -`);
}();

JavaScript Function Definitions

https://www.w3schools.com/js/js_function_definition.asp

JavaScript Logical Operators

https://www.w3schools.com/js/js_operators.asp

Operator Description && logical and || logical or ! logical not

javascript logical operators

JavaScript Comparison and Logical Operators

https://www.w3schools.com/js/js_comparisons.asp

JavaScript Bitwise Operators

https://www.w3schools.com/js/js_operators.asp

https://www.w3schools.com/js/js_bitwise.asp

javascript bitwise operators

@xgqfrms-GitHub
Copy link
Author

!function(){} & JavaScript Logical Operators

JS 默认 function(){} 不调用 function_name(); 不会执行, ! 非运算符,可以打破这种限制规则,使其 立即执行定义的函数!

IIFE

/**
 * @description `!function() & IIFE`
 * @author xgqfrms
 * @link https://github.com/gildata/RAIO/issues/192
 */

// ES6
(
    () => console.log(`ES6 IIFE!`, this)
)(this);



// ES5
(
    function () {
        console.log(`ES5 IIFE!`, this);
    }
)(this);


(
    function () {
        console.log(`ES5 IIFE!`, this);
    }(this)
);


function fn() {
    console.log(`ES5 IIFE!`, this);
}(this);

!function fn() {
    console.log(`ES5 IIFE!`, this);
}(this);


!function fn() {
    console.log(`ES5 IIFE!`, this);
}();


!function fn() {
    console.log(`ES5 IIFE!`, this);
}();
// true


!function fn() {
    console.log(`ES5 IIFE!`, this);
};
// false

!function() {
    console.log(`ES5 IIFE!`, this);
};
// false

function fn() {
    console.log(`ES5 IIFE!`, this);
};

fn();

https://gist.github.com/xgqfrms-GitHub/cd117d20b7c087c0de820f6c3b542148

http://taobaofed.org/blog/2015/10/28/try-catch-runing-problem/

bug

console.log(arr.map(word => word[0].toUpperCase() + word.slice(1)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment