Home > Web Front-end > JS Tutorial > body text

Examples of techniques for sharing properties and methods through prototype attributes in JavaScript_javascript techniques

WBOY
Release: 2016-05-16 16:09:48
Original
1091 people have browsed it

The specific code is as follows:

Copy code The code is as follows:

//Define function
function people(name,sex,age){
this.name = name;
this.sex = sex;
this.age = age;
}

//Share isStudent and sayName methods
people.prototype = {
isStudent:true,
sayName:function(){
alert(this.name);
}
}

var people1 = new people('Han Meimei','女',16); //Instantiate object 1
var people2 = new people('Li Lei','Male',17); //Instantiate object 2

//Let two objects say their names through the sharing method
people1.sayName();
people2.sayName();

//Judging from the shared parameters that they are all students
if(people1.isStudent == people2.isStudent)alert('They are all students');

This article also mentions some knowledge about JavaScript objects, which should not be difficult to understand. If you really don’t understand, you can search on Baidu.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!