Skip to content

Instantly share code, notes, and snippets.

@cmstead
Last active March 3, 2021 07:21
Show Gist options
  • Save cmstead/9b407fe0fcd5a28d6e1b56a5b128a193 to your computer and use it in GitHub Desktop.
Save cmstead/9b407fe0fcd5a28d6e1b56a5b128a193 to your computer and use it in GitHub Desktop.
Examples from code questions
<!-- this file goes along with the angularScope.js file -->
<div ng-controller="HelloController">
{{hello}} <!-- This will display: Hello, World!-->
</div>
// https://docs.angularjs.org/guide/scope
// In a controller, $scope is the variable that exposes data,
// functions, and objects to the Angular view. It also provides
// a means to interact with the Angular rendering behaviors.
// There are other behaviors also linked with scopes.
function HelloController($scope) {
this.$scope = $scope;
}
HelloController.prototype = {
initScopeData: function () {
$scope.hello = 'Hello, World!';
}
};
<!-- https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML -->
<div id="container">
<p>This is some text</p>
</div>
<script>
// innerHTML retrieves all HTML content inside of a selected element
// and returns it as a string
const content = document.getElementById('container').innerHTML
console.log(content) // logs: <p>This is some text</p>
</script>
<!--
https://docs.angularjs.org/api/ng/directive/ngRepeat
Suppose we have an array of fruits: ['peach', 'mango', 'loquat', 'pear']
ng-repeat (an AngularJS directive) will loop through the array and output
an element in the DOM for each array element.
Consider the following:
-->
<ul>
<li ng-repeat="fruit in fruits">{{fruit}}</li>
</ul>
<!-- Output -->
<ul>
<li>peach</li>
<li>mango</li>
<li>loquat</li>
<li>pear</li>
</ul>
<!-- https://docs.angularjs.org/guide/expression#one-time-binding -->
<!--
One-time binding in Angular (NOT AngularJS) takes the first data value
sent to the view for display, and applies it to the view. No new applications
of view values will be rendered.
The example in the official docs is a great way to see it in action.
-->
public class MyBaseClass {
// add is overloaded here.
// in this case, add takes a single integer argument
public int add (int x) {
return add(x, 0);
}
// This case is an overload of add.
// Overloading is when you use the same method name
// but change the signature in a unique way.
// In this case, we accept two arguments instead of just one.
public int add (int x, int y) {
return x + y;
}
public int sum (List<int> numbers) {
throw new Exception("Not implemented");
}
}
public class MyInheritingClass extends MyBaseClass {
// This class contains an override for sum
// Overriding is when an inheriting class implements
// the same method name, with the same signature as
// the parent class, but replaces the logic.
public int sum(List<int> numbers) {
return numbers
.stream()
.reduce(0, (total, nextNumber) -> total + nextNumber);
}
}
import React, { Component } from react;
export default class MyComponent extends Component {
state = {
times: 0
};
handleClick = () => {
// console.log("I've been clicked!!");
const newClickCount = this.state.times + 1;
this.setState({
times: newClickCount
})
}
render() {
return (
<div>
<div onClick={this.handleClick}>CLICK ME</div>
<div>I have been clicked {this.state.times} times</div>
</div>
);
}
}

Primary key

Primary Key Article

A primary key is the value which can be used to directly refer to a record in a table. It's typically a number, and must be unique.

Describing Elements of the Table

Information Schema Article

I think this one is the INFORMATION_SCHEMA, though I'm not sure what their intent is. INFORMATION_SCHEMA returns information about the table you query against. See the following example:

You can do something like this:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "My_Table"

Index/Indexes/Indices

Indexes in SQL

Index values are a means for uniquely identifying and accessing a record quickly. Indexes are (ideally) faster than using a primary key.

I'm starting to fall asleep at the keyboard so I need to stop here. I hope this helps!

// I don't know why we are directly instantiating an object since
// we could be using something nice like Spring, but whatever.
// Here is instantiating an object:
public class MyObject {
public static MyObject getNewMyObject() {
// Instantiation (informal: creating a new object, or newing)
MyObject myObjectInstance = new MyObject();
return myObjectInstance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment