当前位置: 首页 > news >正文

深圳专业网站制作技术营销策划与运营团队

深圳专业网站制作技术,营销策划与运营团队,商标logo创意免费一键生成,网站建设的创新之处目录 一、构造函数 1、 创建对象 2、new执行过程 3、带参数构造函数 4、实例成员与静态成员 二、内置构造函数 1、Object静态方法 2、包装类型 3、Array 1、map方法 2、find方法 3、findIndex( ) 4、some与every 5、reverse() 6、reduce方法 7、forEach() …

目录

一、构造函数

   1、 创建对象

2、new执行过程

 3、带参数构造函数

4、实例成员与静态成员

二、内置构造函数

1、Object静态方法

 2、包装类型

3、Array

1、map方法  

2、find方法

3、findIndex( ) 

4、some与every

5、reverse()

6、reduce方法     

7、forEach() 方法

8、filter( )


一、构造函数

构造函数是专门用于创建对象的函数,如果一个函数使用 new 关键字调用,那么这个函数就是构造函数。

<script>// 定义函数function foo() {console.log('通过 new 也能调用函数...');}// 调用函数new foo;
</script>

 

总结:

  1. 使用 new 关键字调用函数的行为被称为实例化

  2. 实例化构造函数时没有参数时可以省略 ()

  3. 构造函数的返回值即为新创建的对象

  4. 构造函数内部的 return 返回的值无效!


   1、 创建对象


    
    1.字面量对象

    const obj = { uname: 'John' , age: 20 }

    2.new Object
  

 // const obj = new 0bject({ uname: 'John' , age: 20 })const obj = new 0bject( )obj.uname = 'John'obj.age = 20console.log(obj)


 

    3.通过构造函数(自定义)创建

    构造函数--->1.构造出对象的函数,
             2.将来通过new调用
             3.构造函数名首字母建议大写
 

内置内构函数(Array,Date,Object)
    自定义构造函数

    定义学生构造函数
    function Student( ) {
    //添加属性    this===创建出来的对象
    this.属性名=属性值
    this.方法名=funcion() {  }

    //不需要写return,默认会返回this,假如显示指定return
    // return基本类型会被忽略, return引用类型将来new得到的也是该引用类型 
    //return [  ]
    
    }
 

 //  定义学生构造函数function Student() {// 添加属性this.school = '大前端学院'this.age = 18}ƒ// 基于Student构造函数创建对象const s1 = new Student()const s2 = new Student()console.log(s1)console.log(s2)

调用
    const s1=new Student( )

    无参数时  小括号可以省略
    const s1=new Student

2、new执行过程

new 关键字来调用构造函数-->得到一个对象
    1.实际参数传递给形式参数
    2.内部先创建一个空对象 {  },并且让this指向该空对象
    3.执行函数体
    4.返回这个对象

 //  定义学生构造函数function Student() {this.school = '大前端学院'this.age = 18this.sayHi = function () {console.log('sayHi')}}const s1 = new Student()console.log(s1)s1.sayHi()const s2 = new Student // 无参数 小括号可省略console.log(s2)

 3、带参数构造函数

   function Student(age){this.name='张三'this.age=agethis.speek=function() {console.log('speek')}}const s1=new Student(20)console.log(s1)console.log(s1.age)

4、实例成员与静态成员

通过构造函数创建的对象称为实例对象,实例对象中的属性和方法称为实例成员。

实例(对象)   new出来的对象叫实例对象    new过程即实例化对象过程

    实例成员指的是new出来的对象中的属性或方法
    访问:对象 . 属性名
     school age sayHi 都叫实例成员
    const s1 = new Student( 19)
    console.log(s1.school)

    静态成员   通过构造函数.属性=值
    通过构造函数.属性去访问    

    Student.nation = 'china'
      console.log ( Student.nation)

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><script>// 实例(对象) new出来的对象叫实例对象 new过程即实例化对象过程// 实例成员指的是new出来的对象中的属性或方法function Student(age) {// 添加属性 this===创建出来的对象this.school = '大前端学院'this.age = agethis.sayHi = function () {console.log('sayHi')}}// school age sayHi 都叫实例成员const s1 = new Student(19)console.log(s1.school)// 静态成员 通过构造函数.属性  = 值// 通过构造函数.属性去访问 Student.nation = 'china'console.log(Student.nation)// Date.now() 静态方法</script></body>
</html>

总结:

  1. 构造函数内部 this 实际上就是实例对象,为其动态添加的属性和方法即为实例成员

  2. 为构造函数传入参数,动态创建结构相同但值不同的对象

注:构造函数创建的实例对象彼此独立互不影响。

 

在 JavaScript 中底层函数本质上也是对象类型,因此允许直接为函数动态添加属性或方法,构造函数的属性和方法被称为静态成员。

<script>// 构造函数function Person(name, age) {// 省略实例成员}// 静态属性Person.eyes = 2Person.arms = 2// 静态方法Person.walk = function () {console.log('^_^人都会走路...')// this 指向 Personconsole.log(this.eyes)}
</script>

总结:

  1. 静态成员指的是添加到构造函数本身的属性和方法

  2. 一般公共特征的属性或方法静态成员设置为静态成员

  3. 静态成员方法中的 this 指向构造函数本身

 

 

二、内置构造函数

内置的构造函数:Array   Object    Date    String    Number
    Function  创建函数


静态方法:Object.keys()
    Object.values()
    Object.assign()
    Array.isArray()
    Date.now()
    Array.from()

1、Object静态方法

Object.keys(obj)  获取所有属性组成的数组

 0bject.values(obj)        获取所有属性值组成的数组
 
    Object.assign ->ES6新增方法
    可以对多个元对象进行复制

    Object.assign(复制的对象,复制的源对象...)
 

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><script>const obj = {name: '华为p60 pro',price: 6999,color: 'purple',}// ['name', 'price', 'color']//  ['华为p60 pro',6999,'purple']// const keys = []// const values = []// for (let k in obj) {//   keys.push(k)·  //   values.push(obj[k])// }// console.log(keys, values)const keys = Object.keys(obj) // 获取所有属性组成的数组const values = Object.values(obj) //  获取所有属性值组成的数组console.log(keys, values)console.log(values.join('-'))//   Object.assign -> ES6新增方法const o = {}const o1 = { name: 'longge' }const o2 = { age: 18 }// o1 o2 源对象//  assign实现对象的拷贝const r = Object.assign(o, o1, o2)console.log(o)</script></body>
</html>

总结:

  1. 推荐使用字面量方式声明对象,而不是 Object 构造函数

  2. Object.assign 静态方法创建新的对象

  3. Object.keys 静态方法获取对象中所有属性

  4. Object.values 表态方法获取对象中所有属性值

 

 2、包装类型

const str = 'hello'   //const str = new String( 'hello ')//[]-> new Array ( )const r = str.substring(1)// str.substring(1)   str = nullconsole.log(r)

3、Array

Array 是内置的构造函数,用于创建数组  

数组的方法

1、map方法  

 高阶函数----函数的参数接受一个函数或返回值是函数的函数

    arr.map(function( item,index, arr) {// item -数组每一个元素//index -数组元素的索引/ / arr-----原数组})
  const arr=[10,20,30,40]const newArray=arr.map(function(item,index,arr){return item*2})console.log(newArray)//[20, 40, 60, 80]

 

map  映射   
    变异方法
      map对原数组循环,每次函数返回值会放入新数组中
      map不影响原数组

2、find方法

    find( ) 查找满足条件的第一个元素,  找到就返回该元素,找不到是undefined
    返回的是满足条件的元素

    arr.find ( function ( item, index , arr) {})
 const arr=[1,3,7,8,4,2,9,3,6,8]const ele=arr.find(function(item,index,arr){return item===3})console.log(ele)//3

 


3、findIndex( ) 


findIndex( )   查找满足条件的第一个元素的索引,找到就返回该元素的索引,找不到是-1
indexOf( ) 只能找具体的值

    arr.findIndex ( function ( item, index , arr) {})
    const arr=[22,34,66,22,6,7,9,0,6]const index=arr.findIndex(function(item,index,arr){return item===6})console.log(index)//4

 


4、some与every

    some对数组进行循环,发现满足条件的第一个元素则循环结束返回true,  假如所有元素不满足返回false

  

  const arr = [1,-3,20,9,11,12]//数组中是否含有偶数的元素const flag = arr.some(function (item) {return item % 2 === 0})console.log(flag)//true


every对数组进行循环,所有元素都满足返回true,假如遇到第一个不满足的元素结束,返回false,

     const arr=[3,6,8,9,-3,-6,9]const flag = arr.every(function (item) {console.log(item)return item > -9})console.log(flag)//true

 


5、reverse()

 翻转数组

    对原数组进行翻转

 const arr=[1,2,3,4,5,6]console.log(arr.reverse())//[6, 5, 4, 3, 2, 1]
6、reduce方法     
    const arr = [1,2,3,4]arr.reduce( function (prev,current,index, arr) {console.log( prev,current, index)return prev + current})

    第一次 prev指向第一个元素, current指向第二个元素, index是current指向的元素的下标
    第二次prev代表上一次函数返回值,current继续指向下一个元素
    ... .
    最后一次函数的返回值作为reduce最终的结果

    /*
    prev->1 current->2 return 3
    prev->3 current->3 return 6
    prev->6 current->4return 10
    */

arr.reduce(function(prev , current) { },初始值)
记忆:指定初始值,prev第一次就指向该初始值,以后的prev是上一次函数返回值, current指向第一个元素
      没有指定初始值,prev第一次就指向数组第一个元素,current指向第二个元素,以后的prev是上一次函数返回值


7、forEach() 方法


    forEach代替for循环
 

   arr.forEach(function (item,index, arr) {console.log(item)})
    const arr=[2,4,6,8,9,44,22]let sum=0arr.forEach(function(item){sum+=item})console.log(sum)//95

 


forEach没有返回值   影响原数组

8、filter( )

过滤  把符合条件的元素组成一个新数组

  

  const newArr = arr.filter(function (item,index) {return item > 9})console.log( newArr)


 


文章转载自:
http://raphia.qwfL.cn
http://normalcy.qwfL.cn
http://bialy.qwfL.cn
http://ruffianize.qwfL.cn
http://calculability.qwfL.cn
http://roose.qwfL.cn
http://carlowitz.qwfL.cn
http://betsy.qwfL.cn
http://joltily.qwfL.cn
http://wherry.qwfL.cn
http://straddle.qwfL.cn
http://signary.qwfL.cn
http://posteen.qwfL.cn
http://scobicular.qwfL.cn
http://bossiness.qwfL.cn
http://swallow.qwfL.cn
http://snell.qwfL.cn
http://allodially.qwfL.cn
http://blackish.qwfL.cn
http://sari.qwfL.cn
http://watchman.qwfL.cn
http://reis.qwfL.cn
http://bespeckle.qwfL.cn
http://fluoroplastic.qwfL.cn
http://congenial.qwfL.cn
http://medic.qwfL.cn
http://pinkster.qwfL.cn
http://isoprenaline.qwfL.cn
http://allod.qwfL.cn
http://gippy.qwfL.cn
http://statuary.qwfL.cn
http://bioastronautic.qwfL.cn
http://forty.qwfL.cn
http://hengest.qwfL.cn
http://kvass.qwfL.cn
http://jumbotron.qwfL.cn
http://oblivion.qwfL.cn
http://toposcopy.qwfL.cn
http://ultramicroscope.qwfL.cn
http://magisterial.qwfL.cn
http://audiolingual.qwfL.cn
http://sufism.qwfL.cn
http://sororial.qwfL.cn
http://inventory.qwfL.cn
http://scientist.qwfL.cn
http://commissariat.qwfL.cn
http://renegotiation.qwfL.cn
http://diageotropism.qwfL.cn
http://assheadedness.qwfL.cn
http://enroot.qwfL.cn
http://iced.qwfL.cn
http://wireworm.qwfL.cn
http://unpeopled.qwfL.cn
http://gassed.qwfL.cn
http://divot.qwfL.cn
http://cassowary.qwfL.cn
http://insociable.qwfL.cn
http://ostrichlike.qwfL.cn
http://synthesise.qwfL.cn
http://unanalysable.qwfL.cn
http://clod.qwfL.cn
http://bodyshell.qwfL.cn
http://puppetize.qwfL.cn
http://osteopathist.qwfL.cn
http://pesthole.qwfL.cn
http://oversimple.qwfL.cn
http://textualism.qwfL.cn
http://hypnotism.qwfL.cn
http://ferocious.qwfL.cn
http://restatement.qwfL.cn
http://marcia.qwfL.cn
http://hexose.qwfL.cn
http://soundful.qwfL.cn
http://phonation.qwfL.cn
http://intone.qwfL.cn
http://graywacke.qwfL.cn
http://assessable.qwfL.cn
http://equanimously.qwfL.cn
http://frivolously.qwfL.cn
http://napoleonist.qwfL.cn
http://metronidazole.qwfL.cn
http://brothel.qwfL.cn
http://cgh.qwfL.cn
http://viscerogenic.qwfL.cn
http://becomingly.qwfL.cn
http://curvesome.qwfL.cn
http://phenakistoscope.qwfL.cn
http://tricerium.qwfL.cn
http://fleche.qwfL.cn
http://progressive.qwfL.cn
http://prolegomenon.qwfL.cn
http://auditoria.qwfL.cn
http://education.qwfL.cn
http://telautogram.qwfL.cn
http://sandarac.qwfL.cn
http://drum.qwfL.cn
http://wayang.qwfL.cn
http://binaural.qwfL.cn
http://motorable.qwfL.cn
http://supercharger.qwfL.cn
http://www.15wanjia.com/news/88074.html

相关文章:

  • 做网站是否需要自购服务器百度seo排名查询
  • 辅助购卡网站怎么做seo最新技巧
  • 做网站登录的需求分析今日热点新闻15条
  • 微信公众号手机网站网站推广怎样做
  • 网站租空间多少钱网推接单平台
  • 免费com网站域名注册百度小说搜索风云榜排行榜
  • 自己搭建小型服务器惠州seo按天计费
  • 慧聪网网站建设策略赣州是哪个省
  • 建设团队网站百度关键词推广价格查询
  • 做网站报价出名的优化排名推广关键词
  • 帮朋友做网站的坑营销推广活动方案
  • 网站圣诞问候特效怎样推广自己的网站
  • 一些房产网站是怎么做的友情链接买卖平台
  • 网站建设需要什么硬件搜索引擎谷歌
  • wordpress去除缓存青岛网站快速排名优化
  • 给金融公司群做网站合法吗seo方案书案例
  • 昆明网站建设电话亚马逊免费的关键词工具
  • 广告最多的浏览器seo工作职位
  • 环保设备公司网站模板东莞网站建设公司
  • 电商平台网站制作费用新手怎么推广自己的店铺
  • 上海网站设计与开发公司网络营销策划ppt范例
  • 没有网站可以做app吗1+x网店运营推广
  • 外贸视频网站开发成都网络营销公司
  • 嘉兴网站优化找相似图片 识别
  • 网店美工课程seo值是什么意思
  • 安微建设厅网站查架子工真假seo中文含义
  • 做网站需要多少钱平邑巢湖seo推广
  • 做网站开发哪种语言更稳定高效营销型网站设计制作
  • 后台管理系统网站模板职业培训机构管理系统
  • 两个网站做的h5如何合在一起营销方案策划