首页 文章 精选 留言 我的

精选列表

搜索[javascript引擎],共10011篇文章
优秀的个人博客,低调大师

JavaScript 原型

原型对象:每一个对象都从原型继承属性 原型的存在 所有通过对象直接量创建的对象都具有同一个原型对象,通过Object.prototype获得对该原型对象的引用 通过new关键字和构造函数创建的对象的原型就是构造函数的prototype属性的值,当然通过new Object()创建的对象也继承自Object.prototype 通过Object.create()创建的对象使用第一个参数作为创建对象的原型 没有原型的对象为数不多,Object.prototype就是其中之一,不继承任何属性 所有的内置构造函数都具有一个继承自Object.prototype的原型,所以Array.prototype继承自Object.prototype,由new Array()创建的Array对象的属性同时继承自Array.prototype和Object.prototype。则一系列原型对象链接起来构成了我们所说的原型链 原型的作用 类继承:原型对象的属性被类的所有实例所继承,如果原型对象的值是函数,这个函数就做作为类的实例方法调用 原型的访问 类访问原型对象的方式:ClassName.prototype 类的实例访问原型对象的方式:ObjectName.constructor.prototype(ObjectName.__ proto__)

优秀的个人博客,低调大师

JavaScript 链表

数组的大小是固定的,从数组的起点或者中间插入或移除项,成本很高,因为需要移动元素,Array类方法的背后是同样的问题。 链表存储有序的元素集合,但不同于数组,链表中的元素在内存中不是连续的,每个元素由一个存储元素本身的节点,和一个指向下一个元素的引用组成(指针、链接)。 声明: function LinkedList(){ var Node = function(element){//辅助类 this.element = element; this.next = null; } var length = 0; var head = null; //在链表末尾插入 this.append = function(element){ var node = new Node(element); var current; if(head === null){ head = node; }else{ current = head; while(current.next){ current = current.next; } current.next = node; } length++; } //在任意位置插入一个元素 this.insert = function(position,element){ //检查越界值 if(position>= 0 && position<= length){ var node = new Node(element); var current = head; var previous; var index = 0; //在第一个位置添加 if(position === 0){ node.next = current; head = node; }else{ while(index++ < position){ previous = current; current = current.next; } node.next = current; previous.next = node; } length++; return true; }else{ return false; } } //从链表的特定位置移除一项 this.removeAt = function(position){ //检查越界值 if(position>-1 && position<length){ var current = head; var previous ; var index = 0; //如果传入的位置为第一项 if(position === 0){ head = current.next;//移除第一项 }else{ while(index++ < position){ previous = current; current = current.next; } //将previous与current的下一项链接起来:跳过current,从而移除它 previous.next = current.next; } length--; }else{ return null;//越界则表示没有该项 } } //删除指定元素 this.remove = function(element){ var index = this.indexOf(element); return this.removeAt(index) } //找到指定元素,找到则返回位置,没找到返回-1 this.indexOf = function(element){ var current = head; var index = 0; //这里的index设置为几,那么获取元素的位置就从几开始,设置为1则第一个元素的位置就返回1 while(current){ if(element === current.element){ return index; } index++; current = current.next; } return -1; } //返回链表是否为空 空位true 非空位false this.isEmpty = function(){ return length === 0; } //返回链表长度 this.size = function(){ return length; } //只输出末尾对象的内容 this.toString = function(){ var current = head; var string = ''; while(current){ string += current.element + ','; current = current.next; } return string; } this.getHead = function(){ return head; } } 实例化调用: var list = new LinkedList(); list.append(5); console.log('长度:'+list.size()) list.append(10); console.log('长度:'+list.size()) console.log('内容:'+list.toString()) console.log('头部内容:'+list.getHead().element) list.append(15); list.append(20); list.append(25); list.append(30); console.log('长度:'+list.size()) console.log('是否为空:'+list.isEmpty()) console.log('10在哪里:'+list.indexOf(10)) console.log(list.toString()) list.remove(20) console.log(list.toString()) list.removeAt(2); console.log('长度:'+list.size()) console.log(list.toString()) 打印结果:

优秀的个人博客,低调大师

JavaScript

栈是一种遵从后进先出(LIFO)原则的有序集合。 新添加的或待删除的元素都保存在栈的末尾,称作栈顶,另一端就叫栈底。 我们在这里先定义一个栈: function Stack(){ let items = [];//存储栈 //添加一个或多个元素到栈顶 this.push = function(element){ items.push(element) } //移除栈顶的元素,并且返回被移除的元素 this.pop = function(){ return items.pop()//pop方法有返回值 } //返回栈顶的元素,不做任何操作 this.peek = function(){ return items[items.length-1]; } //检测栈里是否有元素,没有true,有false this.isEmpty = function(){ return items.length === 0; } //清空栈 this.clear = function(){ items = [] } //返回栈里的元素个数 this.size = function(){ return items.length; } } 在上面我们已经定义好了一个栈,现在我们来调用一下这个栈: let stack = new Stack(); console.log("stack栈是否为空:"+stack.isEmpty());//true stack.push(5);//(压栈) stack.push(10); stack.push(15); stack.push(20); console.log("stack栈顶元素:"+stack.peek())//20 console.log("stack栈长度:"+stack.size())//4 stack.pop();//删除栈顶元素(出栈) console.log("stack栈顶元素:"+stack.peek())//15 console.log("stack栈是否为空:"+stack.isEmpty());//false stack.clear(); console.log("stack栈是否为空:"+stack.isEmpty());//true 通过上述的方法,我们就能够比较明显的看出来栈的效果啦

优秀的个人博客,低调大师

JavaScript遍历循环

定义一个数组和对象 const arr = ['a', 'b', 'c', 'd', 'e', 'f']; const obj = { a: 1, b: 2, c: 3, d: 4 } for() 经常用来遍历数组元素 遍历值为数组元素索引 for (let i = 0, len = arr.length; i < len; i++) { console.log(i); // 0 1 2 3 4 5 console.log(arr[i]); // a b c d e f } forEach() 用来遍历数组元素 第一个参数为数组元素,第二个参数为数组元素索引,第三个参数为数组本身(可选) 没有返回值 arr.forEach((item, index) => { console.log(item); // a b c d e f console.log(index); // 0 1 2 3 4 5 }) map() 用来遍历数组元素 第一个参数为数组元素,第二个参数为数组元素索引,第三个参数为数组本身(可选) 有返回值,返回一个新数组 every(),some(),filter(),reduce(),reduceRight()不再一一介绍 let arrData = arr.map((item, index) => { console.log(item); // a b c d e f console.log(index); // 0 1 2 3 4 5 return item; }) console.log(arrData); // ["a", "b", "c", "d", "e", "f"] for...in 可循环对象和数组,推荐用于循环对象 1.循环值为对象属性 for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log(key); // a b c d 属性 console.log(obj[key]); // 1 2 3 4 属性值 } } 2.值为数组索引 for (let index in arr) { console.log(index); // 0 1 2 3 4 5 数组索引 console.log(arr[index]); // a b c d e f 数组值 } 当我们给数组添加一个属性name arr.name = '我是自定义的属性' for (let index in arr) { console.log(index); // 0 1 2 3 4 5 name (会遍历出我们自定义的属性) console.log(arr[index]); // a b c d e f 我是自定义属性name } for...of 可循环对象和数组,推荐用于遍历数组 1.遍历值为数组元素 for (let value of arr) { console.log(value); // a b c d e f 数组值 } 2.遍历对象时须配合Object.keys()一起使用,直接用于循环对象会报错,不推荐使用for...of循环对象 循环值为对象属性 for (let value of Object.keys(obj)) { console.log(value); // a b c d 对象属性 } 总结 用于遍历数组元素使用:for(),forEach(),map(),for...of 用于循环对象属性使用:for...in

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册