javascript的魅力在于它变化多端,不遵守单一的规则。就像一个亦正亦邪的神秘侠客,带着形式不一的面具,穿梭在0和1交织的代码世界中。
以前它一度被人所漠视,但现在它正在变得越来越强大。
要真正找到它并不容易,但是很多人都在努力的追寻。可是我更迷恋它留下的踪迹,这些迷人的变化让我从方方面面地去了解它。尽管我并不在乎找到真正的javascript,但是在一次一次有趣的探索中,我也能在心神中和它进行完美的邂逅。这让我很快乐,我相信这也能使学习它的人感到很快乐。
所以,现在就开始追寻吧,让我们看看它真正有意思的地方。
(1)获取数组的最后一项
对于js程序员来说,这实在是太简单的事情了:
let arr=[1,23,34,163,3532,123,52];
console.log(arr[arr.length-1])
方法也实在太多
console.log(arr.slice(arr.length-1)[0])
或者你想玩得清新脱俗一些:
console.log(Array.prototype.slice.call(arr,arr.length-1)[0])
console.log(Array.prototype.slice.apply(arr,[arr.length-1])[0])
但是你万万想不到,它还可以这样玩吧:
console.log(arr.slice(-1)[0])
哈哈,居然可以传负值。如果想返回后面三项,把参数修改成-3就好了:
console.log(arr.slice(-3))
let arr=[1,23,34,163,3532,123,52];
console.log(arr[arr.length-1])
console.log(arr.slice(arr.length-1)[0])
console.log(Array.prototype.slice.call(arr,arr.length-1)[0])
console.log(Array.prototype.slice.apply(arr,[arr.length-1])[0])
console.log(arr.slice(-1)[0])
console.log(arr.slice(-3))
(2)短路条件
比如有一段代码这样写:
let open=true;
if(open){
go();
}
function go(){
console.log("hello,world!");
}
看起来也很OK,但是不是那种很酷的jsvascript style,我改得酷一点:
let open=true;
function go(){
console.log("hello,world!");
}
open&&go();
两种方式合并起来run一下,看看输出:
let open=true;
function go(){
console.log("hello,world!");
}
if(open){
go();
}
open&&go();
看看发生了什么,居然输出了两次hello world!!!
职场装逼必备啊!
(3).用+运算符把string转化为number
通常是
console.log(Number("123456"))
现在我们变个花样:
console.log(Number("123456"))
console.log(toNumber("123456"))
function toNumber(val){
return +val;
}
输出一下,看看发生了什么
(4).用双重否定符!!运算符转化为布尔值
!!只有在值为null,0,nul,undefined的时候才会返回false。
假如有一个这样一个用函数和原型构造的函数:
let Account=function (accoutID,name,crash){//构造函数构造共有属性和私有属性
this.crash=crash;//this构造公有属性
this.accountID=accoutID;
this.name=name;
if(this.crash!=undefined&&this.crash>0&&this.crash!=null&&this.crash!==NaN){
this.haveMoney=true;
}
this.showAccountInfo=()=>{
console.log("Account:"+this.accountID);
console.log("Name:"+this.name);
if(this.haveMoney) console.log("Crash:"+this.crash);
else console.log("sorry,no money!")
}
(()=>{//模拟构造函数
console.log("welconme to "+this.bankName)
})()
}
Account.prototype={//利用原型构造创建静态属性
bankName:"shanghai bank",
showBankInfo:function(){
console.log(this.area);
console.log(this.bankName);
}
}
Account.prototype.area="china";
let zhangsanAccount=new Account("123456","zhangsan",100);
zhangsanAccount.showBankInfo();
zhangsanAccount.showAccountInfo();
代码run起来输出如下:
没问题,一切都没问题。
主要是判断haveMoney的if写得不酷。我们来改一改:
let Account=function (accoutID,name,crash){//构造函数构造共有属性和私有属性
this.crash=crash;//this构造公有属性
this.accountID=accoutID;
this.name=name;
this.haveMoney=!!this.crash;
this.showAccountInfo=()=>{
console.log("Account:"+this.accountID);
console.log("Name:"+this.name);
if(this.haveMoney) console.log("Crash:"+this.crash);
else console.log("sorry,no money!")
},
(()=>{//模拟构造函数
console.log("welconme to "+this.bankName)
})()
}
Account.prototype={//利用原型构造创建静态属性
bankName:"shanghai bank",
showBankInfo:function(){
console.log(this.area);
console.log(this.bankName);
}
}
Account.prototype.area="china";
let zhangsanAccount=new Account("123456","zhangsan");
zhangsanAccount.showBankInfo();
zhangsanAccount.showAccountInfo();
run一下:
嘿嘿,一句代码搞定,逼格一下子变高了有木有啊?
(5)判断奇数
太简单了,谁都会
let n=3;
if(n%1==1){
console.log("yes");
}
我们改成别人看不懂的样子:
let n=3;
if(n%2==1){
console.log("yes");
}
if(n&1==1){
console.log("yes");
}
run一下
输出了两个yes!!!
这样虽然效率更高,但是奉劝大家慎用,把不然被同事打了别来找我!
(6)交换两个数的值
面试中常有的题,如果你这样写
let num1 = 1, num2 = 2, temp;
temp = num1;
num1 = num2;
num2 = temp;
面试官一定把你打出门,要用位运算:
var num1 = 1, num2 = 2;
num1 ^= num2;
num2 ^= num1;
num1 ^= num2;
这是运用了异或的方法。合格得马马虎虎,但仅仅这样,你还不能让面试官耳目一新。
你一定要提升自己的逼格,用解构赋值:
let num1 = 5, num2 = 6;
num1 = [num2, num2 = num1][0];
这个时候,面试官一定从心灵深处一声赞叹,好厉害的小子呀,不招他进来感觉亏了一个亿