日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript学习笔记——对象知识点

發(fā)布時(shí)間:2025/6/17 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript学习笔记——对象知识点 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

javascript對(duì)象的遍歷、內(nèi)存分布和封裝特性

一、javascript對(duì)象遍歷

1.javascript屬性訪問
對(duì)象.屬性
對(duì)象[屬性] //字符串格式

//javascript屬性的訪問方法var ren ={};ren.name="張三";ren.sex="男";ren.eat=function () {alert("吃飯");}alert(ren.name);alert(ren["name"]);

2.javascript屬性遍歷
for in

//javascript屬性遍歷var ren ={};ren.name="張三";ren.sex="男";ren.eat=function () {alert("吃飯");}for (var i in ren) {alert(ren[i])}

?通過arguments來遍歷傳入的參數(shù)

function myArray () {var lengs= arguments.length;for (var i=0; i<lengs; i++) {this[i]=arguments[i];}}var arr=new myArray(1,2,3);alert(arr[0]);


二、內(nèi)存分布


三、對(duì)象的特性之封裝
把對(duì)象所有的組成部分組合起來,盡可能的隱藏對(duì)象的部分細(xì)節(jié),使其受到保護(hù)。
只保留有限的接口和外部發(fā)生聯(lián)系。

  一、工廠函數(shù)

//工廠函數(shù)function dianshi (color,size,brand) {var Tv={};Tv.color=color;Tv.size=size;Tv.brand=brand;Tv.look=function () {alert("看電視");}Tv.play=function () {alert("玩游戲");}Tv.dvd=function () { alert("DVD");} return Tv;}var ds=dianshi("red","30inch","sony");//alert(typeof ds)alert(ds.color)var ds1=dianshi("blue","40inch","changh");alert(ds1["size"])

  二、構(gòu)造函數(shù) 

//構(gòu)造方法的形式function Tv(color,size,brand) {this.color=color;this.size=size;this.brand=brand;this.play=function () {alert("玩游戲");}this.look=function () {alert("看電視");}this.dvd=function () {alert("DVD");}}var sony=new Tv("red","20 inch","sony");alert(sony.color) 

  三、prototype方法

  對(duì)原型屬性的修改將影響到所有的實(shí)例

//prototype方法function Tv(color,size,brand) {this.color=color;this.size=size;this.brand=brand;this.play=function () {alert("玩游戲");}}Tv.prototype.look=function () {alert("看電視");}Tv.prototype.dvd=function () {alert("DVD");}Tv.prototype.aaa={name:"張三"};var sony=new Tv("red","20 inch","sony");var changhong =new Tv("red","20 inch","CH"); // delete sony.color // delete sony.play // delete sony.look // alert(sony.color) // alert(sony.play) // alert(sony.look) // sony.look(); // changhong.look();alert(sony.aaa.name="李四"); alert(changhong.aaa.name);

  四、混合方法

//混合方式 function Tv(color,size,brand) {this.color=color;this.size=size;this.brand=brand;this.play=function () {alert("玩游戲");}Tv.prototype.aaa={name:"張三"};}Tv.prototype.look=function () {alert("看電視");}Tv.prototype.dvd=function () {alert("DVD");}

?

javascript對(duì)象的繼承和Object對(duì)象

對(duì)象的一個(gè)類可以從現(xiàn)有的類中派生,并且擁有現(xiàn)有的類的方法或是屬性,這和過程叫做繼承。被繼承的類叫做父類或是基類,繼承的類叫做子類。

(一個(gè)對(duì)象擁有另一個(gè)對(duì)象的屬性和方法)


優(yōu)點(diǎn):

提高代碼的重用性

提高代碼的可維護(hù)性

提高代碼的邏輯性


一、Object對(duì)象

var obj=new Object()

屬性:

  1.constructor
  對(duì)創(chuàng)建對(duì)象的函數(shù)的引用(指針)。

//1.constructor //對(duì)創(chuàng)建對(duì)象的函數(shù)的引用(指針)var obj=new Object();alert(obj.constructor)


  2.Prototype 原型

  **********************************************
  對(duì)該函數(shù)對(duì)象的對(duì)象原型的引用。是函數(shù)對(duì)象的默認(rèn)屬性

  **********************************************

//Prototype //對(duì)該函數(shù)對(duì)象的對(duì)象原型的引用。var obj=new fun1();function fun1 () {this.name="zhangsan";}alert(obj.prototype)alert(fun1.prototype)

  A.對(duì)象的共享屬性存放到代碼段當(dāng)中。

  B.可以實(shí)現(xiàn)繼承。

方法:

  A.hasOwnProperty(property)
  判斷對(duì)象是否有某個(gè)特定的屬性,返回true或者false

alert(obj.hasOwnProperty("name"))

  B.IsPrototypeOf(object)
  判斷該對(duì)象是否為另一個(gè)對(duì)象的原型。(用來檢測對(duì)象的類型)

  var arr=new Array();
  alert(Array.prototype.isPrototypeOf(arr))

  c.運(yùn)算符
  instanceof

  ?java 中的instanceof 運(yùn)算符是用來在運(yùn)行時(shí)指出對(duì)象是否是特定類的一個(gè)實(shí)例

alert(arr instanceof Array)


二、繼承

1.原型繼承

function person () {this.name="張三";this.say=function () {alert(this.name)} }function student () { } student.prototype=new person()var zhangsan=new student (); zhangsan.say()

2.對(duì)象冒充的形式
  A.call
  obj1.fun.call(obj2,參數(shù)1......)

  B.apply
  obj1.fun.call(obj2,[參數(shù)1,參數(shù)2....])

  讓對(duì)象1的方法冒充成對(duì)象2的方法。

//對(duì)象冒充 /* function person () {this.name="張三";this.say=function () {alert(this.name)} }function student () {this.name="李四"; } var ren=new person (); var zhangsan=new student ();ren.say.call(zhangsan)*/ function person (name) {this.name=name;this.say=function () {alert(this.name)} }function student () {window.person.apply(this,["zhangsan"]) }var zhangsan=new student ();alert(zhangsan.name)zhangsan.say();

??

對(duì)象的繼承順序

一、對(duì)象的繼承順序

//對(duì)象的繼承順序Object.prototype.say=function () {alert("我是頂層的方法");}function person () {this.say=function () {alert("我是父類的方法");}}person.prototype.say=function () {alert("我是父類原型的方法");}function study () {this.say=function () {alert("本身的方法");}}study.prototype=new person();study.prototype.say=function () {alert("本身原型的方法");} var zhangsan=new study ();alert(zhangsan.say)

?


?

轉(zhuǎn)載于:https://www.cnblogs.com/tonglin0325/p/4712985.html

總結(jié)

以上是生活随笔為你收集整理的JavaScript学习笔记——对象知识点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。