js冲刺一下

js中__proto__和prototype的区别和关系

  

  1.对象有属性__proto__,指向该对象的构造函数的原型对象。
  2.方法除了有属性__proto__,还有属性prototype,prototype指向该方法的原型对象。

 

深入浅出妙用 Javascript 中 apply、call、bind

  ***两道面试题***

关于js中伪数组

  我们可以通过Array.prototype.slice.call(fakeArray)将伪数组转变为真正的Array对象。

JS的splice()方法和slice()方法

  1.如何利用splice方法实现数组去重

  2.es6去重数组

// 去除数组的重复成员
[...new Set(array)]
或者
Array.from(new Set(array));

 

ES6入门

 

js日期格式化

function formatDate(date) {var fmt="yyyy-MM"var o = {"M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //"h+": date.getHours(), //小时 "m+": date.getMinutes(), //"s+": date.getSeconds(), //"q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 
        };if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));for (var k in o)if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));return fmt;}
getNowFormatDate(date) {var seperator1 = "-";var seperator2 = ":";var month = date.getMonth() + 1;var strDate = date.getDate();if (month >= 1 && month <= 9) {month = "0" + month;}if (strDate >= 0 && strDate <= 9) {strDate = "0" + strDate;}var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate+ " " + date.getHours() + seperator2 + date.getMinutes()+ seperator2 + date.getSeconds();return currentdate;}

js Promise

  简单测试一下, then函数可以返回新的promise。resolve()函数的返回值会传递到then(resolve, reject)函数中的resolve方法中。

Promise((resolve)=>{resolve(()=>{return 'xixi';})}).then((fun)=>{console.log(fun()); return Promise((resolve)=>{resolve(()=>{return 'haha'})}).then((fun)=>{console.log(fun())})});

  理解 Promise 简单实现的背后原理

//一个简单的Promise实现方案
function
Promise(fn) {var value = null;var events = [];this.then = function(f) {events.push(f); return this;}function resolve(newValue) {var f = events.shift();f(newValue, resolve);}fn(resolve); } function a() { return new Promise(function(resolve) {console.log("get...");setTimeout(function() {console.log("get 1");resolve(1);}, 1000)}); } a().then(function(value, resolve) {console.log("get...");setTimeout(function() {console.log("get 2");resolve(2);}, 1000) }).then(function(value, resolve) {console.log(value) })这样就得到控制台如下的结果 get... get 1 get... get 2 2

 

  JavaScript进阶之路——认识和使用Promise,重构你的Js代码 。一个较为复杂的Promise实现方案如下。

// see => http://promises-aplus.github.io/promises-spec/
// see => https://github.com/cujojs/when/blob/master/lib/makePromise.js

;(function(root, factory) {if (typeof module !== 'undefined' && module.exports) {// CommonJSmodule.exports = factory();} else if (typeof define === 'function' && define.amd) {// AMD / RequireJS
        define(factory);} else {root.Promise = factory.call(root);}
}(this, function() {'use strict';function Promise(resolver) {if(!(this instanceof Promise)) return new Promise(resolver);this.status = 'pending';this.value;this.reason;// then may be called multiple times on the same promisethis._resolves = [];this._rejects = [];if(isFn(resolver)) resolver(this.resolve.bind(this), this.reject.bind(this));return this;};Promise.prototype.then = function(resolve, reject) {var next = this._next || (this._next = Promise());var status = this.status;var x;if('pending' === status) {isFn(resolve) && this._resolves.push(resolve);isFn(reject) && this._rejects.push(reject);return next;}if('resolved' === status) {if(!isFn(resolve)) {next.resolve(resolve);} else {try {x = resolve(this.value);resolveX(next, x);} catch(e) {this.reject(e);}}return next;}if('rejected' === status) {if(!isFn(reject)) {next.reject(reject);} else {try {x = reject(this.reason);resolveX(next, x);} catch(e) {this.reject(e);}}return next;}};Promise.prototype.resolve = function(value) {if('rejected' === this.status) throw Error('Illegal call.');this.status = 'resolved';this.value = value;this._resolves.length && fireQ(this);return this;};Promise.prototype.reject = function(reason) {if('resolved' === this.status) throw Error('Illegal call.');this.status = 'rejected';this.reason = reason;this._rejects.length && fireQ(this);return this;};// shortcut of promise.then(undefined, reject)Promise.prototype.catch = function(reject) {return this.then(void 0, reject);};// return a promise with another promise passing inPromise.cast = function(arg) {var p = Promise();if(arg instanceof Promise) return resolvePromise(p, arg);else return Promise.resolve(arg);};// return a promise which resolved with arg// the arg maybe a thanable object or thanable function or otherPromise.resolve = function(arg) {var p = Promise();if(isThenable(arg)) return resolveThen(p, arg);else return p.resolve(arg);};Promise.all = function(promises) {;};// return a promise which reject with reason// reason must be an instance of Error objectPromise.reject = function(reason) {if(!(reason instanceof Error)) throw Error('reason must be an instance of Error');var p = Promise();p.reject(reason);return p;};function resolveX(promise, x) {if(x === promise) promise.reject(new Error('TypeError'));if(x instanceof Promise) return resolvePromise(promise, x);else if(isThenable(x)) return resolveThen(promise, x);else return promise.resolve(x);};function resolvePromise(promise, promise2) {var status = promise2.status;if('pending' === status) {promise2.then(promise.resolve.bind(promise), promise.reject.bind(promise));}if('resolved' === status) promise.resolve(promise2.value);if('rejected' === status) promise.reject(promise2.reason);return promise;};function resolveThen(promise, thanable) {var called;var resolve = once(function(x) {if(called) return;resolveX(promise, x);called = true;});var reject = once(function(r) {if(called) return;promise.reject(r);called = true;});try {thanable.then.call(thanable, resolve, reject);} catch(e) {if(!called) throw e;else promise.reject(e);}return promise;};function fireQ(promise) {var status = promise.status;var queue = promise['resolved' === status ? '_resolves' : '_rejects'];var arg = promise['resolved' === status ? 'value' : 'reason'];var fn;var x;while(fn = queue.shift()) {x = fn.call(promise, arg);resolveX(promise._next, x);}return promise;};function noop () {};function isFn(fn) {return 'function' === type(fn);};function isObj(o) {return 'object' === type(o);};function type(obj) {var o = {};return o.toString.call(obj).replace(/\[object (\w+)\]/, '$1').toLowerCase();};function isThenable(obj) {return obj && obj.then && isFn(obj.then);};function once(fn) {var called;return function() {if(called) return;fn.apply(this, arguments);called = true;};};return Promise;
}));
View Code

 

转载于:https://www.cnblogs.com/hujunzheng/p/6377797.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531269.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springmvc 源码分析

DispatcherServlet的初始化流程 HandlerMapping - RequestMappingHandlerMapping初始化 DefaultAnnotationHandlerMapping 和RequestMappingHandlerMapping RequestToViewNameTranslator请求到视图名称的转换 (如果没有responsebody&#xff0c;并且没有返回一个view&#xff0…

EntityManager的使用

1、最基础的查询 CriteriaBuilder cb entityManager.getCriteriaBuilder(); CriteriaQuery<User> cq cb.createQuery(User.class); Root<User> root cq.from(User.class); //from User cq.select(root); //select * from User javax.persistence.criteria.Predi…

Jackson序列化实例

参考文章 Jackson使用ContextualSerializer在序列化时获取字段注解的属性 使用BeanSerializerModifier定制jackson的自定义序列化(null值的处理) 关于使用ContextualSerializer的补充 BeanSerializerFactory中有如下代码&#xff0c; 关于设置SerializerModifier&#xff0c;如…

maven自定义脚手架(快速生成项目)

Maven之自定义archetype生成项目骨架 利用脚手架生成 新项目 命令行方式 mvn archetype:generate \ -DarchetypeGroupIdcom.xxx \ -DarchetypeArtifactIdarchetype-spring-boot \ -DarchetypeVersion1.0.0 \ -DgroupIdcom.xxx \ -DartifactIddemo-archetype-generate \ -Dversi…

JSONObject 自定义过滤配置

一、自定义过滤器说明 PropertyPreFilter 根据PropertyName判断是否序列化   PropertyFilter 根据PropertyName和PropertyValue来判断是否序列化   NameFilter 修改Key&#xff0c;如果需要修改Key,process返回值则可   ValueFilter 修改Value   BeforeFilter 序列化时…

springmvc防止重复提交拦截器

一、拦截器实现&#xff0c;ResubmitInterceptorHandler.java import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.Hand…

Mac idea 快捷键

Mac键盘符号和修饰键说明 ⌘ Command⇧ Shift⌥ Option⌃ Control↩︎ Return/Enter⌫ Delete⌦ 向前删除键&#xff08;FnDelete&#xff09;↑ 上箭头↓ 下箭头← 左箭头→ 右箭头⇞ Page Up&#xff08;Fn↑&#xff09;⇟ Page Down&#xff08;Fn↓&#xff09;Home Fn ←…

cas4.2.7实现单点登录

准备前参考&#xff1a;  cas server下载地址 cas client 下载地址 安全cookie setSecure详解 Spring通过构造方法注入的四种方式 cas 学习博文 自定义登录页和登录认证 cas server端的login-webflow详细流程 CAS服务端自定义数据库认证用户 准备工作 1. cas server下载之后解…

log4j之log4j2.xml使用

依赖jar包 log4j-api-2.6.2.jar log4j-core-2.6.2.jar log4j-slf4j-impl-2.6.2.jar slf4j-api-1.7.12.jar 在resources目录下新建log4j2.xml&#xff0c;内容如下。 <?xml version"1.0" encoding"UTF-8"?><!--status : 这个用于设置log4j2自身内…

cas4.2.7与shiro进行整合

准备工作 cas单点登录开始前准备&#xff0c;请参考cas4.2.7实现单点登录。 与shiro进行整合 注&#xff1a;准备工作的基础上&#xff0c;对cas客户端进行如下改进。 引入相关jar包 shiro-cas-1.2.6.jar shiro-core-1.2.6.jar shiro-spring-1.2.6.jar shiro-web-1.2.6.jar web…

命令行fuck神器

文章 thefuck git thefuck 转载于:https://www.cnblogs.com/hujunzheng/p/6935587.html

springmvc配置MappingJackson2HttpMessageConverter实现属性驼峰和下划线的转换

需求 php调用java接口时&#xff0c;因为php那边的属性都是下划线风格&#xff0c;java这边的属性都是驼峰的风格。配置springmvc的json转换&#xff0c;在requestBody的时候&#xff08;调用对象的set 方法&#xff09;将java属性name映射成下划线形式 和 请求的参数匹配&…

springmvc中使用MockMvc测试controller

示例代码 import com.alibaba.fastjson.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springfra…

swagger restful api form映射实体对象和body映射实体对象配置

实体Model ModelAttribute一个具有如下三个作用&#xff1a; ①绑定请求参数到命令对象&#xff1a;放在功能处理方法的入参上时&#xff0c;用于将多个请求参数绑定到一个命令对象&#xff0c;从而简化绑定流程&#xff0c;而且自动暴露为模型数据用于视图页面展示时使用&…

程序员的开发文档

Web版&#xff1a; DevDocs API Documentation 桌面版&#xff1a;devdocs-app 转载于:https://www.cnblogs.com/hujunzheng/p/7015947.html

ssh端口转发(之kettle ssh方式连接数据库)

ssh参数解释 格式  ssh [user]host [command] 选项&#xff1a; -1&#xff1a;强制使用ssh协议版本1&#xff1b; -2&#xff1a;强制使用ssh协议版本2&#xff1b; -4&#xff1a;强制使用IPv4地址&#xff1b; -6&#xff1a;强制使用IPv6地址&#xff1b; -A&#xff1a…

ThreadLocal和InheritableThreadLocal使用

InheritableThreadLocal代码 public class InheritableThreadLocal<T> extends ThreadLocal<T> {protected T childValue(T parentValue) {return parentValue;}ThreadLocalMap getMap(Thread t) {return t.inheritableThreadLocals;}void createMap(Thread t, T f…

mybatis generator修改默认生成的sql模板

相关连接&#xff1a; mybatis-generator扩展教程系列 -- 自定义sql xml文件 git项目地址 转载于:https://www.cnblogs.com/hujunzheng/p/7110510.html

oauth简单使用

一、oauth原理参考 理解OAuth 2.0 二、本例中采用授权码模式 大致流程 &#xff08;A&#xff09;用户访问客户端&#xff0c;后者将前者导向认证服务器。  &#xff08;B&#xff09;用户选择是否给予客户端授权。  &#xff08;C&#xff09;假设用户给予授权&#xff0c…

我眼中的服务提供和服务消费

服务提供和消费脑图 服务提供和消费脑图 参见: 服务提供者, 服务消费者, 服务注册中心 服务提供者 1.服务提供者启动&#xff0c;解析xml文件中配置的服务&#xff0c;这里使用Dom4j解析。 2.将服务的一些相关信息注册到 服务注册中心。 注&#xff1a;服务相关信息&#xff1a…