这个BLOG不错,我练习了一下代码。
感觉他是个高手。
========================
http://www.w3cfuns.com/notes/17398/35b250e9b392675c44f4f0cd833b72c8.html
===================================
<html> <head> <title>Test</title> <script> var girl = { name: ‘小雪‘ }; var boy = { name: ‘小帅‘, pay: function() { console.log(‘花了一千元‘); } }; Object.setPrototypeOf(girl, boy); girl.pay(); var a = { fn1: function() { console.log(1); } }; var b = { fn2: function() { console.log(2); } }; var c = { fn3: function() { console.log(3); } }; var d = { fn4: function() { console.log(4); } }; Object.setPrototypeOf(d, c); Object.setPrototypeOf(c, b); Object.setPrototypeOf(b, a); d.fn1(); d.fn2(); d.fn3(); d.fn4(); var a = { sayName: function() { alert(this.name); } }; var laoyao = { name: ‘laoyao‘ }; Object.setPrototypeOf(laoyao, a); //laoyao.sayName(); var laoyao = { name: ‘laoyao‘, sayName: function() { alert(this.name); } }; //var fenshen = {}; //Object.setPrototypeOf(fenshen, laoyao); var fenshen = Object.create(laoyao); console.log(fenshen); //fenshen.sayName(); var createPerson = function(name) { return { name: name, sayName: function() { alert(this.name); } }; }; var laoyao = createPerson(‘laoyao‘); //laoyao.sayName(); var createPerson = function(name) { var o = {}; o.name = name; var proto = { sayName: function() { alert(this.name); } }; Object.setPrototypeOf(o, proto); return o; }; var laoyao = createPerson(‘laoyao‘); //laoyao.sayName(); var createPerson = function(name) { var o = {}; o.name = name; Object.setPrototypeOf(o, createPerson.proto); return o; }; createPerson.proto = { sayName: function() { alert(this.name); } }; var laoyao = createPerson(‘laoyao‘); laoyao.sayName(); </script> </head> <body> test </body> </html>
时间: 2024-10-25 19:36:10