js 构造函数_原型_练习

function a(name) {
//	var this = window	// 不new,正常执行
// 	this = Object.creat(a.prototype);	// new执行,改变this指向
//	this = {	// new执行的后果
//		__proto__ : {	// = a.prototype
// 			constructor : a,
//			__proto__ : {...}	// = Object.prototype
// 		}
// 	}

	this.name = name;

// 	return this;
}

// 函数被定义就会有的,console.log(window)查看
// f a = {
// 	arguments : null,
// 	caller : null,
// 	length : 1,
// 	name : 'a',
// 	prototype : {
// 		constructor : a,
// 		__proto__ : {...}	// = Object.prototype
// 	},
// 	__proto__ : {...},	// = Object.prototype
// 	[[FunctionLocation]] : xx.html:29,
// 	[[scope]] : {0: window}
// }


// a.prototype = {		// 函数被定义时产生的
// 	constructor : a,
// 	__proto__ : {...}	// = Object.prototype
// }
a.prototype.last = 'zh';	// 相当于添加属性
// a.prototype = {		// 结果
// 	last : 'zh',
// 	constructor : a,
// 	__proto__ : {...}	// = Object.prototype
// }

// a.prototype = {		//相当于重新赋给属性
// 	last : 'zh'
// }
// a.prototype = {		// 结果(缺了constructor)
// 	last : 'zh',
// 	__proto__ : {...}	// = Object.prototype
// }

var b = new a('chl');
// b = a {
// 	name : 'chl',
// 	__proto__ : {	// = a.prototype
// 		last : 'zh',
// 		constructor : a,
// 		__proto__ : {...}	// = Object.prototype
// 	}
// }

var c = new Object();	// 或 c = {}
// c = {
// 	__proto__ : {...}	// = Object.prototype
// }

// var c = {
// 	num : 5
// }
// var c = {
// 	num : 5
// 	__proto__ : {...}	// = Object.prototype
// }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些 JS 高级原型原型链的练习题,希望对您有所帮助。 1. 创建一个构造函数 Person,它有属性 name 和 age。创建一个对象 john,它的 name 是 "John",age 是 30。使用 Person 的原型对象添加一个方法 sayHello,它会在控制台输出 "Hello, my name is {name}!"。 ```javascript function Person(name, age) { this.name = name; this.age = age; } Person.prototype.sayHello = function() { console.log(`Hello, my name is ${this.name}!`); }; const john = new Person("John", 30); john.sayHello(); ``` 2. 创建一个构造函数 Animal,它有属性 name 和 type。创建一个对象 cat,它的 name 是 "Whiskers",type 是 "Cat"。使用 Object.create() 方法创建一个对象 dog,它的 name 是 "Fido",type 是 "Dog"。使用 Animal 的原型对象添加一个方法 sayType,它会在控制台输出 "{name} is a {type}."。 ```javascript function Animal(name, type) { this.name = name; this.type = type; } Animal.prototype.sayType = function() { console.log(`${this.name} is a ${this.type}.`); }; const cat = new Animal("Whiskers", "Cat"); const dog = Object.create(Animal.prototype, { name: { value: "Fido", writable: true, enumerable: true, configurable: true }, type: { value: "Dog", writable: true, enumerable: true, configurable: true } }); cat.sayType(); // Whiskers is a Cat. dog.sayType(); // Fido is a Dog. ``` 3. 创建一个构造函数 Shape,它有属性 color 和 name。创建一个对象 circle,它的 color 是 "red",name 是 "Circle"。创建一个对象 square,它的 color 是 "green",name 是 "Square"。使用 Shape 的原型对象添加一个方法 describe,它会在控制台输出 "This is a {color} {name}."。 ```javascript function Shape(color, name) { this.color = color; this.name = name; } Shape.prototype.describe = function() { console.log(`This is a ${this.color} ${this.name}.`); }; const circle = new Shape("red", "Circle"); const square = new Shape("green", "Square"); circle.describe(); // This is a red Circle. square.describe(); // This is a green Square. ``` 希望这些练习题能够帮助您巩固 JS 高级原型原型链的知识。如果您有任何问题或需要进一步的帮助,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值