当前位置:网站首页>JS inheritance prototype
JS inheritance prototype
2022-07-07 09:26:00 【-Coffee-】
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;}
.head{font-size:12px;padding:6px 0 0 10px;}
#login_box{width:300px;height:150px;background:#eee;
border:1px solid #ccc;position:absolute;left:50%;top:50%;margin-left:-150px;margin-top:-75px;display:none;}
#login_box p{height:20px;border-bottom:1px solid #ccc;font-size:12px;padding:6px 0 0 5px;font-weight:bold;}
#close{width:14px;height:14px;background:url(close.png) no-repeat;position:absolute;right:4px;top:6px;cursor: pointer;background-color: red;}
</style>
<!--<script>
window.οnlοad=function(){
var login_btn=document.getElementById('login'),
login_box=document.getElementById('login_box'),
close=document.getElementById('close');
}
function addEvent(ele,type,hander){
}
function showLogin(){
if(ele.addEventListener){
ele.addEventListener(ele,type,hander)
}
else if(ele.attachEvent){
ele.attachEvent('on'+type,hander)
}
else{
ele['on'+type]=hander;
}
}
</script>-->
</head>
<body>
<div class="head"> Pro - , Hello! !<input type="button" value=" deng record " id="login"></div>
<div id="login_box">
<p> The user login </p ><span id="close"> close </span>
</div>
<script>
var person = {
age:26,
name:'wang',
say:function (num) {
console.log ("my name is "+this.age+",my name is "+this.name+'.');
//console.log ("my name is "+person.age+",my name is "+person.name+'.');
console.log(" Pass in a parameter :"+num);
}
}
/*
* The object above There are three properties age name say
* One attribute is method Such as say
* If we want to read his age attribute direct console.log(person.age);
* If we want to add a new attribute Such as job person.job = "coder";
* if we want to change the property(age),just as : person.age = 29;
* For the third Say It uses this This keyword , I will talk about it alone in the future , Here you can understand it as this Namely person This object But it is not recommended to use person
*
* The problem is coming. : Set up person A new attribute It is a method , This method stores a numeric parameter , This parameter is the age you want to set . take person The age of ,
* Revise it and then call person Of say Method .
* */
//person.say(5);
/*
* The above knowledge points
* object Modification of object properties Object method call Object this Function parameter
* */
function Person(option) {
this.age = option.age;
this.name = option.name;
this.say = function () {
console.log ("my name is "+this.age+",my name is "+this.name+'.');
//console.log ("my name is "+person.age+",my name is "+person.name+'.');
}
}
var person = new Person({age:18,name:'wang'});
person.say();
// Why don't we write down ?
function Person1(age,name) {
this.age = age;
this.name = name;
this.say = function () {
console.log ("my name is "+this.age+",my name is "+this.name+'.');
//console.log ("my name is "+person.age+",my name is "+person.name+'.');
}
}
/*
* You can think if there are many parameter names , And we forgot the order of parameters
* */
var person2 = new Person1('wang',18);
person2.say();// What will this output ? You'll find that name And age The assignment is reversed ; Using objects does not have this problem
// But if you use objects , What should we do if we want to add a default value to it ?
function Person3(option) {
option = option || {};
this.age = option.age || 99;
this.name = option.name || 'congjun';
this.say = function () {
console.log ("my name is "+this.age+",my name is "+this.name+'.');
//console.log ("my name is "+person.age+",my name is "+person.name+'.');
}
}
var person3 = new Person3();
person3.say();// Test what will be output by yourself ?
/*
* Be careful || This is a kind of Unitary judgment take option = option || {};
* Come on To the right of the equal sign If option There is , Just put option Assign to... To the left of the equal sign option If it doesn't exist Then put {} Assign to... To the left of the equal sign option
* */
/*
* The above function is called constructor
* The first letter of the function name should be capitalized ; When you use it We need to add a new keyword
* In constructor this Point to the constructor itself
* Ordinary Function? ? Point to window
*
* We can define an object like this var a = new Object(); It's fine too var a = {};
* We can define an array like this var a = new Array(); It's fine too var a = [];
* We can define a date like this var a = new Date(); wait
*
* Although we are programming Both adopt the latter , Because it's easier
* But you need to know , They all come from constructors
*
* var a = new Object(); Take this sentence for example ,a And new Object() What is the relationship between ?
* a yes new Object() Instantiation
*
* Pay attention to the following statement
*
* a There will be one. __proto__ attribute The constructor will have a prototype attribute
* a Of __proto__ Will inherit Its constructor prototype attribute
* The above is the simplest object inheritance .
* But things are far from over
*
* because a Constructor for It could be Some constructor example
* And a constructor It may also be an instance of other constructors
* In this way, there is an inheritance relationship layer by layer
* What is the top ?
* That's it Object object , To Object It's over
* Layers of inheritance are like chains , We call it prototype chain
* You must fully understand the definition of prototype inheritance and prototype chain .
*
* */
// The topic of the third day Use the constructor method to make the topic of the next day z
// Mainly this Prototype And Prototype chain h
/*
* One more thing
*
* */
function Student() {
this.age = 55;
this.teacher = 'JS';
this.major = 'math';
}
Student.prototype.coderName = "congjun";
var student = new Student();
console.log(student.coderName);// This coderName Attributes are inherited
// The following code , To understand , about for in Cycles don't need special attention , But there are some important functions
for(var i in student){
console.log(" All attributes contain inheritance :"+i);
if(student.hasOwnProperty(i)){
console.log(" It has its own attributes :"+i);
}
}
</script>
</body>
</html>
边栏推荐
- Several stages of PMP preparation study
- JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
- Information Security Experiment 2: using x-scanner scanning tool
- Detailed learning notes of JVM memory structure (I)
- The use of recycling ideas
- Unity shader (data type in cghlsl)
- Redis common commands
- H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
- (3/8) method parameters of improper use of enumeration (2)
- Self awakening from a 30-year-old female programmer
猜你喜欢
数据建模中利用3σ剔除异常值进行数据清洗
Mysql database transaction learning notes
Pytest installation (command line installation)
[SVN] what is SVN? How do you use it?
Unity shader (learn more about vertex fragment shaders)
NVIC interrupt priority management
Install pyqt5 and Matplotlib module
Register address name mapping
Storage of data in memory
C language pointer (exercises)
随机推荐
Pick up the premise idea of programming
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
Jenkins task grouping
创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
Cesium load vector data
Yapi test plug-in -- cross request
DRF authentication, permissions, and flow restrictions (only for views in DRF)
Locust performance test 4 (custom load Policy)
Kubernetes cluster capacity expansion to add node nodes
What is MD5
(3/8) method parameters of improper use of enumeration (2)
Sublime Text4 download the view in bower and set the shortcut key
What are the conditions for applying for NPDP?
How to speed up video playback in browser
Pycharm create a new file and add author information
Why is access to the external network prohibited for internal services of the company?
stm32和电机开发(从单机版到网络化)
Netease cloud wechat applet
Some pit avoidance guidelines for using Huawei ECS