var add=function(x1){
var sum=x1;
var fun=function(x2){
sum+=x2;
return fun;
}
fun.toString=function(){
return sum;
}
return fun;
}
alert(add(1)(2)(3));
var a=20;
function fun(){
this.a=50;
return function(){
return this.a
}
}
var m=new fun();
console.log(m());
var n=fun();
console.log(n());
function deepClone(oldObj){
if(oldObj==null){
return null;
}
if(typeof oldObj!=="object"){
return oldObj;
}
var newObj=Array.isArray(oldObj)?[]:{};
for(var key in oldObj){
newObj[key]=deepClone(oldObj[key]);
}
return newObj;
}
var total=0;
Promise.all([
ajax({
url:"http://localhost:5050",
data:"type=a"
}),
ajax({
url:"http://localhost:5050",
data:"type=b"
}),
ajax({
url:"http://localhost:5050",
data:"type=c"
}),
]).then(arr=>{
for(var obj of arr){
total+=obj.count;
}
console.log(total);
})
function ajax({url,type="get",data}){
return new Promise(function(resolve,reject){
var xhr=new XMLHttpRequest();
if(type=="get"&&data!==undefined){
url+="?"+data;
}
xhr.open(type,url,true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
var result=JSON.parse(xhr.responseText)
resolve(result);
}
}
if(type=="post"){
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
if(type=="post"){
xhr.send(data);
}else{
xhr.send();
}
})
}
Array.isArray(obj)
Object.prototype.toString.call(obj)
obj.constructor==Array
obj instanceof Array
Object.getPrototypeOf(obj)==Array.prototype
Array.prototype.isPrototypeOf(obj)
obj.__proto__==Array.prototype
function f1() {
var sum = 0;
function f2() {
sum++;
return f2;
}
f2.valueOf = function () {
return sum;
};
f2.toString = function () {
return sum + '';
};
return f2;
}
console.log(+f1());
console.log(+f1()())
console.log(+f1()()())
function sid(arr){
arr[0]=arr[2];
}
function bar(a,b,c){
c=10;
sid(arguments);
return a+b+c;
}
console.log(bar(1,1,1));
var name="window";
var p={
name:"Perter",
getName:function(){
var self=this;
return function(){
return self.name;
}
}
}
var getName=p.getName();
var _name=getName();
console.log(_name);
this.b="2";
function abc(){
let b=1;++b
setTimeout(()=>{
test('fun test');
},0);
setTimeout(
test('test fun')
,1000);
console.log(b);
function test(str){
this.b++;
console.log(str);
console.log(this.b++);
}
}
abc();
var arr=[];
for(var i=0;i<100000;i++){
arr.push(parseInt(Math.random()*10000));
}
function unique1(arr){
var obj={};
for(var n of arr){
obj[n]=1;
}
var newArr=[];
for(var key in obj){
newArr.push(parseInt(key));
}
return newArr;
}
console.time("unique1");
unique1(arr);
console.timeEnd("unique1");
function unique2(arr){
var set=new Set(arr);
var newArr=[...set];
return newArr;
}
console.log(unique2(arr))
console.time("unique2");
unique2(arr);
console.timeEnd("unique2");
<button>click me</button>
<button>click me</button>
<button>click me</button>
<button>click me</button>
<button>click me</button>
<script>
var btns=document.getElementsByTagName("button");
for(var i=0;i<btns.length;i++){
(function(i){
btns[i].onclick=function(){
console.log(i);
}
})(i);
}
for(let i=0;i<btns.length;i++){
btns[i].onclick=function(){
console.log(i);
}
}
</script>
var arr=[1,2,3,"a",4,"e"];
for(var i=arr.length-1;i>=0;i--){
if(typeof arr[i]==="number"){
arr[i]++;
}else{
arr.splice(i,1);
}
}
console.log(arr);
var arr1=[1,3,7,9,12,37,45];
var arr2=[2,4,9,13,45,88,92];
for(var i=0,j=0,result=[];i<arr1.length&&j<arr2.length;){
if(arr1[i]<arr2[j]){
i++;
}else if(arr1[i]>arr2[j]){
j++;
}else{
result.push(arr1[i])
i++;
j--;
}
}
console.log(result)
var arr3=[1,2,4,6,7,11,12,15,17];
for(var i=0,j=arr3.length-1;i<j;){
if(arr3[i]+arr3[j]>19){
j--;
}else if(arr3[i]+arr3[j]<19){
i++;
}else{
console.log(`${arr3[i]}+${arr3[j]}`);
i++;
j--;
}
}
var arr=[1,2,3,4,5];
Array.prototype.forEach=function(fun){
for(var i=0;i<this.length;i++){
if(this[i]!==undefined){
fun(this[i],i,this)
}
}
}
arr.forEach(function(elem){
console.log(`${elem}`)
})
Array.prototype.map=function(fun){
var arr=[];
for(var i=0;i<this.length;i++){
if(this[i]!==undefined){
arr[i]=fun(this[i],i,this);
}
}
return arr;
}
var bb=arr.map(function(elem){
return elem*2;
})
console.log(bb)
var str="helloworld";
var arr=str.split("");
var result=arr.reduce(function(prev,elem){
if(prev[elem]===undefined){
prev[elem]=1;
}else{
prev[elem]+=1;
}
return prev;
},{})
console.log(result);
Function.prototype.bind=function(obj){
var fun=this;
var arr1=Array.prototype.slice.call(arguments,1);
return function(){
var arr=Array.prototype.slice.call(arguments);
var arr2=arr1.concat(arguments);
fun.apply(obj,arr2);
}
}
if (!Function.prototype.bind)(function () {
var slice = Array.prototype.slice;
Function.prototype.bind = function () {
var thatFunc = this,
thatArg = arguments[0];
var args = slice.call(arguments, 1);
if (typeof thatFunc !== 'function') {
throw new TypeError('Function.prototype.bind - ' +
'what is trying to be bound is not callable');
}
return function () {
var funcArgs = args.concat(slice.call(arguments))
return thatFunc.apply(thatArg, funcArgs);
};
};
})();
Array.prototype.slice.call(arguments,starti)
[].slice.call(arguments,starti)
function animal(name,age){
this.name=name;
this.age=age;
}
animal.prototype.intr=function(){}
function people(name,age){
animal.call(this,name,age);
}
Object.setPrototypeOf(people.prototype,animal.prototype);
var a={n:1};
var b=a;
a.x=a={n:2};
console.log(a);
console.log(b);
a.n=3;
console.log(b);
console.log(a);
var num=new Array();
for(var i=0;i<4;i++){
num[i]=f1(i);
}
function f1(n){
function f2(){
alert(n);
}
return f2;
}
num[2]();
num[1]();
num[0]();
num[3]();
var str="helloworld";
var arr=[];
for(var i=0;i<str.length;i++){
var char=str[i];
if(arr[char]===undefined){
arr[char]=1;
}else{
arr[char]++;
}
}
var maxChar,count=0;
for(var key in arr){
if(arr[key]>count){
maxChar=key;
count=arr[key];
}
}
var search="?uname=dingding&upwd=123456&favs=swimming&favs=running&favs=basketball";
function search20bj(str){
var obj={};
str=str.slice(1);
var arr=str.split("&");
for(var s of arr){
var [key,value]=s.split("=");
if(obj[key]===undefined){
obj[key]=value;
}else{
obj[key]=[].concat(obj[key],value);
}
}
return obj;
}
console.log(search20bj(search));
function fun(n,o){
console.log(o);
return{
fun:function(m){
return fun(m,n);
}
}
}
var a=fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b=fun(0)
.fun(1)
.fun(2)
.fun(3);
var c=fun(0)
.fun(1);
c.fun(2);
c.fun(3);
function setObj(p){
p.name="red";
p={};
p.name="blue";
}
var p={
name:"pink"
};
setObj(p);
console.log(p);
var a=2;
var obj={
a:4,
fn1:(function(){
this.a*=2;
var a=3;
return function(){
this.a*=2;
a*=3;
console.log(a);
}
})()
}
var fn1=obj.fn1;
console.log(a);
fn1();
obj.fn1();
console.log(a);
console.log(obj.a);
function fun(o){
o.name="red";
o={};
o.name="blue";
}
var obj={name:"white",age:11};
fun(obj);
console.log(obj);
function Foo(){
getName=function(){
console.log(1);
};
return this;
}
Foo.getName=function(){
console.log(2);
}
Foo.prototype.getName=function(){
console.log(3);
}
var getName=function(){
console.log(4);
}
function getName(){
console.log(5);
}
Foo.getName();
getName();
Foo().getName();
getName();
new Foo.getName();
new Foo().getName();
new new Foo().getName();
function A(){ }
function B(){
return new A();
}
A.prototype=new A();
B.prototype=new B();
var a=new A();
var b=new B();
console.log(a.__proto__==b.__proto__);
var x=0;
var foo={
x:1,
bar:function(){
console.log(this.x);
var that=this;
return function(){
console.log(this.x);
console.log(that.x);
}
}
}
foo.bar();
foo.bar()();
function Foo(){
Foo.a=function(){
console.log(1);
}
this.a=function(){
console.log(2);
}
}
Foo.prototype.a=function(){
console.log(3);
}
Foo.a=function(){
console.log(4);
}
Foo.a();
let obj=new Foo();
obj.a();
Foo.a();
var a={};
var b={
key:"a"
};
var c={
key:"c"
};
a[b]="123";
a[c]="456";
console.log(a[b]);
var reg10k = /([a-z]+):\/\/([A-Za-z0-9.]+):(\d+)((\/[a-z.]+)+)\?(([a-z]+=[a-z0-9]+&?)+)#([a-z]+)/i;
var reg18k =/[a-z]+(?=:\/\/)|(?<=:\/\/)[a-z0-9.]+(?=:)|(?<=:)\d+(?=\/)|\/[a-z/.]+(?=\?)|(?<=\?)[a-z0-9=&]+(?=#)|#[a-z0-9]+/ig;
var url = "http://locahost:5500/public/index.html?uname=lena&upwd=123456#top"
var arr10k = url.match(reg10k);
var arr18k = url.match(reg18k);
console.log(arr10k);
console.log(arr18k);
var arr = url.split("://");
var protocol = arr[0];
var rest = arr[1];
var arr = rest.split("#");
var hash = `#${arr[1]}`;
rest = arr[0];
var arr = rest.split("?");
var search = arr[1];
rest = arr[0];
var arr = rest.split("/");
var host = arr.shift();
var pathname = "/" + arr.join("/");
var arr = host.split(":")
var hostname = arr[0];
var port = arr[1];
console.log(protocol);
console.log(host);
console.log(hostname);
console.log(port);
console.log(pathname);
console.log(search);
console.log(hash);
function fun(){
var n=999;
nAdd=function(){n++};
return function(){
console.log(n);
}
}
var getN=fun();
getN();
nAdd();
getN();
function fun(){
for(var i=0,arr=[];i<3;i++){
arr[i]=function(){console.log(i)}
}
return arr;
}
var funs=fun();
funs[0]();
funs[1]();
funs[2]();
var a=10;
(function(){
console.log(a);
a=5;
console.log(window.a);
var a=20;
console.log(a);
})()
function fun(){console.log(1)};
fun();
function fun(){console.log(2)};
fun();
var fun=100;
fun();
|