把随机数对象暴露给window成为全局对象 原型及原型链 原型的指向是否可以改变
生活随笔
收集整理的這篇文章主要介紹了
把随机数对象暴露给window成为全局对象 原型及原型链 原型的指向是否可以改变
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
把隨機(jī)數(shù)對(duì)象暴露給window成為全局對(duì)象
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 通過(guò)自調(diào)用函數(shù)產(chǎn)生一個(gè)隨機(jī)數(shù)對(duì)象,在自調(diào)用函數(shù)外面,調(diào)用該隨機(jī)數(shù)// 對(duì)象方法產(chǎn)生隨機(jī)數(shù)(function(window){var num = 10;// 產(chǎn)生隨機(jī)數(shù)的構(gòu)造函數(shù)function Random(){// 在原型對(duì)象中添加方法Random.prototype.getRandom = function(min,max){return Math.floor(Math.random()*(max-min)+min);};}// 把Random對(duì)象暴露給頂級(jí)對(duì)象window---->外部可以直接使用這個(gè)對(duì)象window.random = new Random();})(window);var num = random.getRandom(0,5);console.log(num);// 全局變量</script> </head> <body></body> </html>原型及原型鏈
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 使用對(duì)象---->使用對(duì)象中的屬性和對(duì)象中的方法,使用對(duì)象就要先有構(gòu)造函數(shù)// 構(gòu)造函數(shù)function Person(name,age){// 屬性this.name = name;this.age = age;// 在構(gòu)造函數(shù)中的方法this.eat = function(){console.log("吃好吃的");};}// 添加共享的屬性Person.prototype.sex = "男";// 添加共享的方法Person.prototype.sayHi = function(){console.log("怎么這么帥");};// 實(shí)例化對(duì)象,并初始化var per = new Person("小明",20);// per.sayHi();// 如果想要使用一些屬性和方法,并且屬性的值在每個(gè)對(duì)象中都是一樣的,// 方法在每個(gè)對(duì)象中的操作也是一樣的,那么,為了共享數(shù)據(jù),節(jié)省內(nèi)存空間// 是可以把屬性和方法通過(guò)原型的方式進(jìn)行賦值// 實(shí)例對(duì)象的結(jié)構(gòu)console.dir(per);// 構(gòu)造函數(shù)的結(jié)構(gòu)console.dir(Person);// 實(shí)例對(duì)象的類型__proto__和構(gòu)造函數(shù)的類型prototype指向是否相同// 實(shí)例對(duì)象中__proto__原型指向的構(gòu)造函數(shù)中的原型prototypeconsole.log(per.__proto__==Person.prototype);// 實(shí)例對(duì)象中__proto__是原型,瀏覽器使用的// 構(gòu)造函數(shù)中的prototype是原型,程序員使用的// 原型鏈:是一種關(guān)系,實(shí)例對(duì)象和原型對(duì)象之間的關(guān)系,關(guān)系是通過(guò)// 原型(__proto__)來(lái)聯(lián)系的</script> </head> <body></body> </html>原型的指向是否可以改變
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// function Student(){// }// Student.prototype.study = function(){// console.log("就是天天學(xué)習(xí)");// };// Student.prototype = {// eat:function(){// console.log("哈哈,好吃的榴蓮");// }// };// var stu = new Student();// stu.eat();// stu.study();// 人的構(gòu)造函數(shù)function Person(age){this.age = 10;}// 人的原型對(duì)象方法Person.prototype.eat = function(){console.log("人的吃");};// 學(xué)生的構(gòu)造函數(shù)function Student(){}Student.prototype.sayHi = function(){console.log("小蘇你好帥哦");};// 學(xué)生的原型,指向了一個(gè)人的實(shí)例對(duì)象Student.prototype = new Person(10);var stu = new Student();stu.eat();// stu.say();// 原型指向可以改變// 實(shí)例對(duì)象的原型__proto__指向的是該對(duì)象所在的構(gòu)造函數(shù)的原型對(duì)象// 實(shí)例對(duì)象的原型__proto__// 構(gòu)造函數(shù)的原型(prototype)如果改變了,實(shí)例對(duì)象的原型(__proto__)指向// 也會(huì)發(fā)生改變// 原型的指向也是可以改變的// 實(shí)例對(duì)象和原型對(duì)象之間的關(guān)系是通過(guò)__proto__原型來(lái)聯(lián)系起來(lái)的,這個(gè)關(guān)系就是// 原型鏈</script> </head> <body></body> </html>?
總結(jié)
以上是生活随笔為你收集整理的把随机数对象暴露给window成为全局对象 原型及原型链 原型的指向是否可以改变的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 为内置对象添加原型方法 把局部变量编程全
- 下一篇: CentOS系统参数优化