当前位置:网站首页>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>
边栏推荐
- VSCode+mingw64+cmake
- Dynamics 365Online ApplicationUser创建方式变更
- Unity uses mesh to realize real-time point cloud (II)
- SiteMesh getting started example
- Why is access to the external network prohibited for internal services of the company?
- 数据库多表关联查询问题
- (3/8) method parameters of improper use of enumeration (2)
- 华为HCIP-DATACOM-Core_03day
- Sublime Text4 download the view in bower and set the shortcut key
- **grafana安装**
猜你喜欢
JVM 内存结构 详细学习笔记(一)
Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
Yapi test plug-in -- cross request
信息安全实验二 :使用X-SCANNER扫描工具
Information Security Experiment 2: using x-scanner scanning tool
Postman interface test (I. installation and use)
C language pointer (Part 1)
Register address name mapping
Mysql数据库-锁-学习笔记
PMP Exam Preparation experience systematically improve project management knowledge through learning
随机推荐
JWT certification used in DRF
Unity shader (learn more about vertex fragment shaders)
Add new item after the outbound delivery order of SAP mm sto document is created?
VSCode+mingw64
Information Security Experiment 3: the use of PGP email encryption software
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
How does the project manager write the weekly summary and weekly plan?
What is the rating of Huishang futures company? Is it safe to open an account? I want to open an account, OK?
STM32 clock system
Mysql数据库-锁-学习笔记
PMP Exam details after the release of the new exam outline
Entity of cesium data visualization (Part 1)
What is the use of PMP certificate?
Sublime Text4 download the view in bower and set the shortcut key
SiteMesh getting started example
JVM garbage collection detailed learning notes (II)
4、 Fundamentals of machine learning
Register address name mapping
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
Information Security Experiment 2: using x-scanner scanning tool