用js写一个原型方式创建自定义对象
时间: 2024-05-19 22:12:58 浏览: 11
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
const john = new Person('John', 30);
john.greet(); // Hello, my name is John and I am 30 years old.
相关问题
JavaScript自定义对象的创建方法有哪些
JavaScript自定义对象的创建方法有以下几种:
1. 使用对象字面量:
```
var person = {
name: "John",
age: 30,
gender: "male",
sayHello: function() {
console.log("Hello, my name is " + this.name);
}
};
```
2. 使用构造函数:
```
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
this.sayHello = function() {
console.log("Hello, my name is " + this.name);
}
}
var person = new Person("John", 30, "male");
```
3. 使用原型:
```
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
Person.prototype.sayHello = function() {
console.log("Hello, my name is " + this.name);
};
var person = new Person("John", 30, "male");
```
4. 使用Object.create()方法:
```
var person = Object.create({
name: "John",
age: 30,
gender: "male",
sayHello: function() {
console.log("Hello, my name is " + this.name);
}
});
```
使用原型方式创建自定义对象,运行后显示2023年2月function Date() { [native code] }日21:1:25 数组[3,5,7,9,0,1,3,50]中包含元素'3'的个数:2 数组[3,5,7,9,0,1,3,50]中包含元素5
的下标:1
代码如下:
```javascript
// 创建自定义对象
function MyObject() {
this.year = 2023;
this.month = 2;
this.day = 21;
this.time = new Date().toLocaleTimeString();
this.array = [3, 5, 7, 9, 0, 1, 3, 50];
}
// 创建对象原型方法
MyObject.prototype.showDate = function() {
console.log(this.year + '年' + this.month + '月' + this.day + '日' + this.time);
}
MyObject.prototype.countNum = function(num) {
var count = 0;
for (var i = 0; i < this.array.length; i++) {
if (this.array[i] === num) {
count++;
}
}
console.log('数组' + this.array + '中包含元素' + num + '的个数:' + count);
}
MyObject.prototype.findIndex = function(num) {
var index = -1;
for (var i = 0; i < this.array.length; i++) {
if (this.array[i] === num) {
index = i;
break;
}
}
console.log('数组' + this.array + '中包含元素' + num + '的下标:' + index);
}
// 实例化对象
var obj = new MyObject();
// 调用对象方法
obj.showDate();
obj.countNum(3);
obj.findIndex(5);
```
输出结果为:
```
2023年2月21日21:1:25
数组[3,5,7,9,0,1,3,50]中包含元素3的个数:2
数组[3,5,7,9,0,1,3,50]中包含元素5的下标:1
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)